开发者

How to Iterate a dictionary of references to matplotlib figures

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 res开发者_C百科ist 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.


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)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜