如何迭代 matplotlib 图形引用的字典
我有许多函数,每个函数都会创建一个或多个图形。创建图形时,将引用添加到字典中,如下所示:
self.figures['figureKey'] = figure()
在另一个函数中,我想迭代该字典并保存每个图形;最好使用字典键作为文件名的一部分或全部。我已经能够迭代字典,但 figure()
函数似乎需要一个与图编号相对应的整数,拒绝由键给出的引用。
for fig in self.figures:
figure(self.figures[fig]) #does not work
figure(fig) #also does not work
savefig(fig) #seems to let me use the key as a filename--nice
我还尝试使用“get_fignums()”并迭代返回的数组,但这会丢失与键名称的关联。也许可以从图形指针中取消引用图形编号?有人对此有巧妙的方法吗?
请抵制以“你为什么不……”开头的倾向。答案是,这对我来说不是一个显而易见的方法。我对此有点陌生。
I have a number of functions each of which creates one or more figures. As the figures are created, a reference is added to a dictionary, like so:
self.figures['figureKey'] = figure()
In another function, I would like to iterate this dictionary and save each of the figures; it would be nice to use the dictionary key as part or all of the filename. I have been able to iterate the dictionary but the figure()
function seems to require an integer corresponding to the figure number, rejecting the reference given by the key.
for fig in self.figures:
figure(self.figures[fig]) #does not work
figure(fig) #also does not work
savefig(fig) #seems to let me use the key as a filename--nice
I have also attempted to use `get_fignums()' and iterate the returned array, but that loses the association with the key names. Perhaps it is possible to dereference the figure number from the figure pointer? Anyone have a slick approach to this?
Please resist the tendency to begin an answer with the phrase "why don't you just..." The answer to that is that it was not an obvious approach to me. I'm sort of new at this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许我对你在做什么感到困惑......如果你只是想保存图形,为什么不使用
fig
对象的savefig
方法?pyplot.savefig
保存活动图窗,但使用特定图窗实例的fig.savefig
方法保存该特定图窗,无论哪个图窗处于活动状态。Maybe I'm confused as to what you're doing... If you just want to save the figure, why not use the
fig
object'ssavefig
method?pyplot.savefig
saves the active figure, but using a particular figure instance'sfig.savefig
method saves that particular figure, regardless of which one is active.我可能在这里遗漏了一些东西(不是真正的 matplotlib 用户),但是您不是将图形对象本身存储在字典中吗?如果是这样,您可以使用
for key, value in self.figures.items()
迭代键和值,然后使用number
获取图形编号属性。作为测试,我使用交互式解释器尝试了以下操作:
它似乎有效:2 个图(一个单调递增,一个单调递减)在当前目录中保存为 PNG 文件 name1.png 和 name2.png。
I might be missing something here (not really a matplotlib user), but aren't you storing the figure objects themselves in the dictionary? If so, you can iterate over both the keys and the values using
for key, value in self.figures.items()
, and then get the figure number using thenumber
attribute.As a test, I tried the following using the interactive interpreter:
And it appeared to work: 2 plots (one monotonically increasing and one monotonically decreasing) were saved as the PNG files name1.png and name2.png in the current directory.
您可以从图形实例中获取图形编号。 来自文档:
因此,要按数字访问数字,您可以这样做:
不过,我现在没有安装 matplotlib 来测试它。
You can get the figure number from the figure instance. From the docs:
So to access a figure by number, you could do this:
I don't have matplotlib installed just now to test that though.