Matplotlib savefig does not save axes
Asked Answered
L

7

34

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 the 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

Lookin answered 24/10, 2013 at 20:46 Comment(0)
G
5

I was having the same problem using Jupyter notebook and the command: %matplotlib notebook. The figure showed correctly in the notebook but didn't print axis and titles when saved with fig.savefig(). I changed %matplotlib notebook to %matplotlib inline and that solved the problem.

Grit answered 17/10, 2017 at 17:16 Comment(0)
C
28

Defining fig = plt.figure(figsize=(15,10)) at the beginning, saving the file as .jpg, and setting bbox_inches='tight' solved the issue for me.

plt.savefig('filename.jpg',bbox_inches='tight', dpi=150)

bbox_inches='tight' seems to fix cropping issues but it didn't work for .png.

Chronopher answered 12/2, 2021 at 15:50 Comment(3)
In a practical sense this answer was best. I changed only from png to jpg, and the resulting image included the axes that I expected. Good enough for today. But it remains frustrating to me to not understand why these are different and to not understand how to output a .png as intended.Weave
Molly's answer below explains why ;)Carlenecarleton
works with .tiff but adding bbox_inches argNmr
N
13

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.

Narceine answered 3/5, 2021 at 12:45 Comment(1)
this is the correct solution for those having issues in jupyter notebooks. thank you so much!Coatbridge
F
10

You are setting the axis to start at the very bottom left of the figure and to fill up the entire thing. There's no room for the axis labels or the title. Try this:

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.1,0.1,0.75,0.75]) # axis starts at 0.1, 0.1
ax.set_title("bleh")
ax.set_xlabel("xlabel")
ax.plot(x, y, 'r--')
fig.savefig("fig.png")

plot with axis showing

Florid answered 24/10, 2013 at 21:3 Comment(4)
+1 Alternately just use ax = fig.add_subplot(1, 1, 1), or better yet, just fig, ax = plt.subplots().Muliebrity
or fig, ax = plt.subplots(1, 1, tight_layout=True) (assuming your version of mpl is new enough)Victoria
or plt.figure(tight_layout=True)Foreignism
Didn't work for me trying to create png file, switching to jpg fixed itShetrit
G
5

I was having the same problem using Jupyter notebook and the command: %matplotlib notebook. The figure showed correctly in the notebook but didn't print axis and titles when saved with fig.savefig(). I changed %matplotlib notebook to %matplotlib inline and that solved the problem.

Grit answered 17/10, 2017 at 17:16 Comment(0)
N
4

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)'.

Noiseless answered 12/7, 2021 at 14:49 Comment(0)
M
3

I was using Jupyter Notebook and Just change .png to .jpg and my problem is solved now Here is my code:

# changing the size of figure to 2X2
plt.figure(dpi=100, figsize=(15, 10))
plt.grid()
#display(plt.plot(year1, ratio1))
x = np.arange(1900, 2020, 5)
plt.xticks(x)
plt.title(ttile)
plt.xlabel('Year')
plt.ylabel('Ratio')
plt.plot(year,ratio)
plt.savefig('books_read.jpg', dpi = 300)

Saved Image from the Code

Majestic answered 26/6, 2022 at 10:18 Comment(1)
Helpful! Changing .png to .jpg worked outside Jupyter as well!Resurrectionist
E
0

Comment out plt.show() and instead call plt.savefig('abc.png').

Emancipated answered 7/1 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.