调用pyplot.show()后保存图形会导致文件为空 [英] Saving a figure after invoking pyplot.show() results in an empty file

查看:123
本文介绍了调用pyplot.show()后保存图形会导致文件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例代码生成一个简单的图,然后将其保存到"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')

推荐答案

如果要在显示图形后保存图形,则需要保留图形实例.调用showplt.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显示). gcfgca g 分别设置 c urrent f 图和当前 a 实例. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆