调用 pyplot.show() 后保存图形会生成一个空文件 [英] Saving a figure after invoking pyplot.show() results in an empty file
问题描述
以下示例代码生成一个简单的绘图,然后将其保存到fig1.pdf",然后显示它,然后再次将其保存到fig2.pdf".第一个图像看起来像预期的那样,但第二个图像是空白的(包含一个白色方块).这里到底发生了什么?行 plt.show()
显然搞砸了,但我不知道是什么/如何!
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')
推荐答案
如果要在显示图形后保存图形,则需要保留图形实例.调用show
后plt.savefig
不工作的原因是当前图形已被重置.
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
在幕后跟踪哪些图形、轴等是当前"的(即尚未使用 show
显示).gcf
和 gca
get current figure 和 current 一个实例,分别.plt.savefig
(以及基本上任何其他 pyplot
方法)只执行 plt.gcf().savefig(...)
.换句话说,获取当前图形实例并调用其 savefig
方法.同样 plt.plot
基本上是 plt.gca().plot(...)
.
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(...)
.
调用show
后,当前"图形和轴的列表为空.
After show
is called, the list of "current" figures and axes is empty.
一般来说,最好直接使用图形和轴实例来绘制/保存/显示/等,而不是使用 plt.plot
等来隐式获取当前图形/轴并在其上绘图.将 pyplot
用于所有事情(尤其是交互式)并没有错,但它可以更轻松地用脚射击自己.
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.
将 pyplot
用于 plt.show()
并生成图形和坐标轴对象,然后直接使用图形或坐标轴方法.(例如 ax.plot(x, y)
而不是 plt.plot(x, y)
等)这样做的主要优点是它是明确的.您知道要在哪些对象上绘图,并且不必推理 pyplot 状态机的作用(尽管理解状态机接口也不难).
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')
如果您更喜欢使用 pyplot
界面来处理所有事情,那么只需在调用 show
之前获取图形实例.例如:
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')
这篇关于调用 pyplot.show() 后保存图形会生成一个空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!