Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib figures disappearing between show() and savefig()

I've kept a set of references to figures in a dictionary so that I could save them later if desired. I am troubled that the saved figures are blank if invoke a show() command and look at them first. Since the show() command blocks and I am not using a spyder-like interpreter, I have to close the figures before I get to savefig()

figures['myfig_1'] = figure()
...
figures['myfig_n'] = figure()
...

#show() #disabling this makes the problem go away
print "Saving:"
for fig in figures:
   figure(figures[fig].number)
   savefig(fig)
   print "Figure " + str(figures[fig].number) + ": " + fig

The print statement here has given me the indication that the dictionary is still intact, which I think means that I have not lost the figure references (they are still returning meaningful numbers in their .number attribute.)

Another twist I have noticed is that when I have done a similar thing in a class, storing the dictionary as a member and dividing the store and save functions into their own methods, this does not happen. Is there something about the way I am closing the figures or storing the data which is making the figures loose their data?

like image 590
2NinerRomeo Avatar asked May 16 '11 15:05

2NinerRomeo


People also ask

Is PLT show () necessary?

Plotting from an IPython shell draw() . Using plt. show() in Matplotlib mode is not required.

Is PLT show () blocking?

Answer #1: show() (not with block=False ) and, most importantly, plt. pause(. 001) (or whatever time you want). The pause is needed because the GUI events happen while the main code is sleeping, including drawing.

Why is matplotlib not showing plot?

It means if we are not using the show() function, it wouldn't show any plot. When we use the show() function in the non-interactive mode. That means when we write the code in the file it will show all the figures or plots and blocks until the plots have been closed.

Does matplotlib overwrite Savefig?

Matplotlib Savefig will NOT overwrite old files.


1 Answers

Generally speaking, in cases like this don't use the interactive matlab-ish state machine interface to matplotlib. It's meant for interactive use.

You're trying to make a figure "active", and creating a new figure instead. It doesn't matter which figure is active, if you just retain the returned figure and/or axis objects and use them directly. (Also, don't use wildcard imports! You will regret it at some later point when you're maintaining your code!)

Just do something like this:

import matplotlib.pyplot as plt
figures = {}

figures['a'] = plt.figure()
ax = figures['a'].add_subplot(111)
ax.plot(range(10), 'ro-')

figures['b'] = plt.figure()
ax = figures['b'].add_subplot(111)
ax.plot(range(10), 'bo-')

plt.show()

for name, fig in figures.iteritems():
    fig.savefig('figure-%s.png' % name)
like image 183
Joe Kington Avatar answered Nov 15 '22 18:11

Joe Kington