matplotlib 图形在 show() 和 savefig() 之间消失

发布于 2024-11-07 12:59:05 字数 633 浏览 0 评论 0原文

我在字典中保存了一组对数字的引用,以便以后需要时可以保存它们。如果调用 show() 命令并先查看它们,我很担心保存的数字是空白的。由于 show() 命令会阻塞,并且我没有使用类似间谍程序的解释器,因此我必须在到达 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

这里的 print 语句有给我指示字典仍然完好无损,我认为这意味着我没有丢失图形引用(它们仍然在 .number 属性中返回有意义的数字。)

我注意到的另一个变化是当我在类中做了类似的事情,将字典存储为成员并将存储和保存函数划分为它们自己的方法时,这种情况不会发生。我关闭数字或存储数据的方式是否导致数字丢失数据?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟若柳尘 2024-11-14 12:59:05

一般来说,在这种情况下,不要使用 matplotlib 的交互式 matlab-ish 状态机接口。它适用于交互式使用。

您试图使一个图形“活跃”,并创建一个新图形。如果您只是保留返回的图形和/或轴对象并直接使用它们,那么哪个图形处于活动状态并不重要。 (另外,不要使用通配符导入!当您维护代码时,您将会后悔!)

只需执行以下操作:

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)

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)
再可℃爱ぅ一点好了 2024-11-14 12:59:05

From the documentation, whether or not the drawing elements are destroyed from show() depends on the backend, and the version of matplotlib. Not having the figures destroyed seems to be available with version 1.1.0. To figure out which backend is in use, use the get_backend() function. In my case, I was using the Qt4Agg backend. By invoking the TkAgg backend, with the call matplotlib.use('TkAgg') the figures were not destroyed before the save. Now to find out how to change the behavior of the Qt4Agg...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文