Search code examples
pythonmatplotlibplotsavefigure

Can't save matplotlib figure to jpeg (it is blank) in jupiter notebook


I can't save my figure to jpeg (or any other) file (it is blank)

x=list(df2['DAYTIME'])
z=list(df2['av100002 - temp Wywiew'])
x3= x[::75]


fig1 = plt.figure()
axes1 = fig1.add_axes([0,30,3.5,1.4])
axes1.set_title('Nawiew')


axes1.plot(x,z, lw=3)

axes1.set_xticks(x3)  

plt.xticks(x3, rotation=60)
fig1.savefig('xx.png', dpi=200)

Solution

  • The position of your axes is wrong and puts the axes off figure.

    try axes1 = fig1.add_subplot() for a quick fix, which creates an axes centered in the figure space.

    If you want to use add_axes() for manual placement of the axes, then the coordinates are given in figure fractions. The coordinate are [left, bottom, width, height] where 0 represent the left/bottom edge of the figure and 1 the right/top edge of the figure.

    by default fig.add_subplot() is equivalent to fig.add_axes([0.125, 0.11, 0.9, 0.88])

    Full code:

    import matplotlib.pyplot as plt
    
    fig1 = plt.figure()
    axes1 = fig1.add_subplot(111)
    axes1.set_title('Nawiew')
    fig1.savefig('xx.png', dpi=200)
    

    enter image description here