简体   繁体   English

matplotlib数字在show()和savefig()之间消失

[英]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. 我很困惑,如果调用show()命令并首先查看它们,保存的数字是空白的。 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() 由于show()命令阻止而且我没有使用类似Spyder的解释器,所以在进入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.) 这里的print语句给了我指示字典仍然完整,我认为这意味着我没有丢失图形引用(它们仍然在它们的.number属性中返回有意义的数字。)

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? 有什么关于我关闭数字或存储数据的方式,这使得数据松散他们的数据?

Generally speaking, in cases like this don't use the interactive matlab-ish state machine interface to matplotlib. 一般来说,在这种情况下,不要使用matplotlib的交互式matlab-ish状态机接口。 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)

From the documentation , whether or not the drawing elements are destroyed from show() depends on the backend , and the version of matplotlib. 文档中 ,绘图元素是否从show()中销毁取决于后端和matplotlib的版本。 Not having the figures destroyed seems to be available with version 1.1.0. 版本1.1.0似乎没有销毁数据。 To figure out which backend is in use , use the get_backend() function. 要确定使用哪个后端 ,请使用get_backend()函数。 In my case, I was using the Qt4Agg backend. 就我而言,我使用的是Qt4Agg后端。 By invoking the TkAgg backend, with the call matplotlib.use('TkAgg') the figures were not destroyed before the save. 通过调用TkAgg后端,调用matplotlib.use('TkAgg') ,数字在保存之前没有被销毁。 Now to find out how to change the behavior of the Qt4Agg... 现在来了解如何改变Qt4Agg的行为......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM