如何迭代 matplotlib 图形引用的字典

发布于 2024-11-06 04:04:20 字数 613 浏览 1 评论 0原文

我有许多函数,每个函数都会创建一个或多个图形。创建图形时,将引用添加到字典中,如下所示:

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 技术交流群。

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

发布评论

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

评论(3

不再见 2024-11-13 04:04:20

也许我对你在做什么感到困惑......如果你只是想保存图形,为什么不使用 fig 对象的 savefig 方法?

pyplot.savefig 保存活动图窗,但使用特定图窗实例的 fig.savefig 方法保存该特定图窗,无论哪个图窗处于活动状态。

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

figures = [plt.figure() for _ in range(10)]

for i, fig in enumerate(figures):
    ax = fig.add_subplot(111)
    ax.plot(x, i*x)
    ax.axis('equal')

for i, fig in enumerate(figures):
    fig.savefig('temp_slope_%02i.png' % i)

Maybe I'm confused as to what you're doing... If you just want to save the figure, why not use the fig object's savefig method?

pyplot.savefig saves the active figure, but using a particular figure instance's fig.savefig method saves that particular figure, regardless of which one is active.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

figures = [plt.figure() for _ in range(10)]

for i, fig in enumerate(figures):
    ax = fig.add_subplot(111)
    ax.plot(x, i*x)
    ax.axis('equal')

for i, fig in enumerate(figures):
    fig.savefig('temp_slope_%02i.png' % i)
凝望流年 2024-11-13 04:04:20

我可能在这里遗漏了一些东西(不是真正的 matplotlib 用户),但是您不是将图形对象本身存储在字典中吗?如果是这样,您可以使用 for key, value in self.figures.items() 迭代键和值,然后使用 number 获取图形编号属性。

作为测试,我使用交互式解释器尝试了以下操作:

>>> import matplotlib.pyplot as plt
>>> figures = {}
>>> figures['name1'] = plt.figure()
>>> plt.plot([1, 2, 3])
>>> figures['name2'] = plt.figure()
>>> plt.plot([6, 5, 4])
>>> for key, figure in figures.items():
...     print 'Saving figure #%d as %s.' % (figure.number, key)
...     plt.figure(figure.number)
...     plt.savefig(key)
...
Saving figure #2 as name2
Saving figure #1 as name1

它似乎有效: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 the number attribute.

As a test, I tried the following using the interactive interpreter:

>>> import matplotlib.pyplot as plt
>>> figures = {}
>>> figures['name1'] = plt.figure()
>>> plt.plot([1, 2, 3])
>>> figures['name2'] = plt.figure()
>>> plt.plot([6, 5, 4])
>>> for key, figure in figures.items():
...     print 'Saving figure #%d as %s.' % (figure.number, key)
...     plt.figure(figure.number)
...     plt.savefig(key)
...
Saving figure #2 as name2
Saving figure #1 as name1

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.

垂暮老矣 2024-11-13 04:04:20

您可以从图形实例中获取图形编号。 来自文档

返回的图形对象有一个保存该数字的数字属性。

因此,要按数字访问数字,您可以这样做:

figure(self.figures[fig].number)

不过,我现在没有安装 matplotlib 来测试它。

You can get the figure number from the figure instance. From the docs:

The returned figure objects have a number attribute holding this number.

So to access a figure by number, you could do this:

figure(self.figures[fig].number)

I don't have matplotlib installed just now to test that though.

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