Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib savefig does not save axes

Tags:

matplotlib

I'm trying to save a figure that works fine in IPython inline but does not save the figure to disk with the axes and titles included.

I am using TKAgg backend by default in matplotlibrc

Any ideas what might be going wrong here? I have clearly set the xlabel and tick marks work correctly in IPython inline plot.

   import matplotlib.pylab as plt  
    x = [1,2,3,3]
    y = map(lambda(x): x * 2, x)
    fig = plt.figure()
    ax = fig.add_axes([0,0,1,1])
    ax.set_title("bleh")
    ax.set_xlabel("xlabel")
    ax.plot(x, y, 'r--')
    fig.savefig("fig.png")

Savefig image without axes labels

like image 367
Richard Todd Avatar asked Oct 24 '13 20:10

Richard Todd


People also ask

Does PLT Savefig overwrite?

Save this question. Show activity on this post. It will create the figure the first time the code is run.

How do I save a figure in matplotlib?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

Why is %Matplotlib inline?

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.


2 Answers

Could be facecolor. I work in jupyter lab, and the facecolor default is set to black, so you don't see the axes, even though they are being drawn.

fig = plt.figure(facecolor=(1, 1, 1))

sets the background color to white.

like image 54
mousomer Avatar answered Sep 28 '22 08:09

mousomer


I was able to solve the issue (in visual studio code jupyter extension) by changing the format from 'png' to 'jpg', along with the parameter 'plt.subplots(tight_layout=True)'.

like image 44
srinivas kumar Avatar answered Sep 28 '22 08:09

srinivas kumar