matplotlib数字消失在show()和savefig()之间 [英] matplotlib figures disappearing between show() and savefig()

查看:41
本文介绍了matplotlib数字消失在show()和savefig()之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在词典中保留了一组对数字的引用,以便以后可以根据需要保存它们.如果调用 show() 命令并首先查看它们,我很担心保存的数字是空白的.由于 show() 命令块并且我没有使用类似 spyder 的解释器,所以我必须在到达 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. 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

这里的打印语句向我表明字典仍然完好无损,这意味着我没有丢失图形引用(它们仍在其 .number 属性中返回有意义的数字.)

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?

推荐答案

来自 documentation,是否从 show() 销毁绘图元素取决于 后端,以及matplotlib的版本.1.1.0 版似乎可以不销毁数字.找出正在使用哪个后端,使用 get_backend()函数.就我而言,我使用的是 Qt4Agg 后端.通过调用TkAgg后端,并调用 matplotlib.use('TkAgg'),图形在保存之前不会被破坏.现在来了解如何更改 Qt4Agg 的行为...

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...

这篇关于matplotlib数字消失在show()和savefig()之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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