matplotlib 图形在 show() 和 savefig() 之间消失
我在字典中保存了一组对数字的引用,以便以后需要时可以保存它们。如果调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,在这种情况下,不要使用 matplotlib 的交互式 matlab-ish 状态机接口。它适用于交互式使用。
您试图使一个图形“活跃”,并创建一个新图形。如果您只是保留返回的图形和/或轴对象并直接使用它们,那么哪个图形处于活动状态并不重要。 (另外,不要使用通配符导入!当您维护代码时,您将会后悔!)
只需执行以下操作:
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:
从 文档 中,无论绘图元素是否被 < code>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 theget_backend()
function. In my case, I was using the Qt4Agg backend. By invoking the TkAgg backend, with the callmatplotlib.use('TkAgg')
the figures were not destroyed before the save. Now to find out how to change the behavior of the Qt4Agg...