Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a figure after invoking pyplot.show() results in an empty file

The following example code generates a simple plot, then saves it to 'fig1.pdf', then displays it, then saves it again to 'fig2.pdf'. The first image looks as expected, but the second one is blank (contains a white square). What's actually going on here? The line plt.show() apparently messes something up, but I can't figure out what/how!

import numpy as np import matplotlib.pyplot as plt x = np.linspace(-1, 1, 100) y = x**2 plt.plot(x,y) plt.savefig('fig1.pdf') plt.show() plt.savefig('fig2.pdf') 
like image 850
andreasdr Avatar asked Feb 19 '14 08:02

andreasdr


People also ask

How do I save a figure after the PLT show?

If you want to save the figure after displaying it, you'll need to hold on to the figure instance. The reason that plt. savefig doesn't work after calling show is that the current figure has been reset.

How do you save a figure in Pyplot?

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.

How do you save a figure in python?

Python hosting: Host, run, and code Python in the cloud! If you want to save matplotlib figures as individual files, you can do this with the savefig function. If you want to save figures in a single file, use the saveas function instead.

What does PLT show () do?

plt. show() starts an event loop, looks for all currently active figure objects, and opens one or more interactive windows that display your figure or figures. The plt. show() command does a lot under the hood, as it must interact with your system's interactive graphical backend.


2 Answers

If you want to save the figure after displaying it, you'll need to hold on to the figure instance. The reason that plt.savefig doesn't work after calling show is that the current figure has been reset.

pyplot keeps track of which figures, axes, etc are "current" (i.e. have not yet been displayed with show) behind-the-scenes. gcf and gca get the current figure and current axes instances, respectively. plt.savefig (and essentially any other pyplot method) just does plt.gcf().savefig(...). In other words, get the current figure instance and call its savefig method. Similarly plt.plot basically does plt.gca().plot(...).

After show is called, the list of "current" figures and axes is empty.

In general, you're better off directly using the figure and axes instances to plot/save/show/etc, rather than using plt.plot, etc, to implicitly get the current figure/axes and plot on it. There's nothing wrong with using pyplot for everything (especially interactively), but it makes it easier to shoot yourself in the foot.

Use pyplot for plt.show() and to generate a figure and an axes object(s), but then use the figure or axes methods directly. (e.g. ax.plot(x, y) instead of plt.plot(x, y), etc) The main advantage of this is that it's explicit. You know what objects you're plotting on, and don't have to reason about what the pyplot state-machine does (though it's not that hard to understand the state-machine interface, either).

As an example of the "recommended" way of doing things, do something like:

import numpy as np import matplotlib.pyplot as plt  x = np.linspace(-1, 1, 100) y = x**2  fig, ax = plt.subplots() ax.plot(x, y) fig.savefig('fig1.pdf') plt.show() fig.savefig('fig2.pdf') 

If you'd rather use the pyplot interface for everything, then just grab the figure instance before you call show. For example:

import numpy as np import matplotlib.pyplot as plt  x = np.linspace(-1, 1, 100) y = x**2  plt.plot(x, y) fig = plt.gcf() fig.savefig('fig1.pdf') plt.show() fig.savefig('fig2.pdf') 
like image 196
Joe Kington Avatar answered Sep 20 '22 21:09

Joe Kington


pyplot.show blocks and destroys the plot upon closing. You can use

plt.show(block=False) 

after which the save to fig2.pdf will work or you can plot it again before saving

plt.plot(x,y) plt.savefig('fig2.pdf') 
like image 28
miles82 Avatar answered Sep 24 '22 21:09

miles82