Python 获取matplotlib中的图形列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3783217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:43:38  来源:igfitidea点击:

Get the list of figures in matplotlib

pythonmatplotlib

提问by Evgeny

I would like to:

我想要:

pylab.figure()
pylab.plot(x)
pylab.figure()
pylab.plot(y)
# ...
for i, figure in enumerate(pylab.MagicFunctionReturnsListOfAllFigures()):
  figure.savefig('figure%d.png' % i)

What is the magic function that returns a list of current figures in pylab?

在 pylab 中返回当前数字列表的魔术函数是什么?

Websearch didn't help...

网络搜索没有帮助...

采纳答案by unutbu

Edit: As Matti Pastell's solution shows, there is a much better way: use plt.get_fignums().

编辑:正如Matti Pastell 的解决方案所示,有一个更好的方法:使用plt.get_fignums().



import numpy as np
import pylab
import matplotlib._pylab_helpers

x=np.random.random((10,10))
y=np.random.random((10,10))
pylab.figure()
pylab.plot(x)
pylab.figure()
pylab.plot(y)

figures=[manager.canvas.figure
         for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
print(figures)

# [<matplotlib.figure.Figure object at 0xb788ac6c>, <matplotlib.figure.Figure object at 0xa143d0c>]

for i, figure in enumerate(figures):
    figure.savefig('figure%d.png' % i)

回答by joaquin

This should help you (from the pylab.figure doc):

这应该对您有所帮助(来自 pylab.figure 文档):

call signature::

figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')

Create a new figure and return a :class:matplotlib.figure.Figureinstance.If num= None, the figure number will be incremented and a new figure will be created.** The returned figure objects have a numberattribute holding this number.

呼叫签名::

图(数字=无,无花果=(8, 6),dpi=80,facecolor='w',edgecolor='k')

创建一个新图形并返回一个 :class:matplotlib.figure.Figure实例。如果num= None,图形编号将增加并创建一个新图形。** 返回的图形对象具有保存此数字的 number属性。

If you want to recall your figures in a loop then a good aproach would be to store your figure instances in a list and to call them in the loop.

如果你想在循环中调用你的图形,那么一个好的方法是将你的图形实例存储在一个列表中并在循环中调用它们。

>> f = pylab.figure()
>> mylist.append(f)
etc...
>> for fig in mylist:
>>     fig.savefig()

回答by mcstrother

Assuming you haven't manually specified numin any of your figure constructors (so all of your figure numbers are consecutive) and all of the figures that you would like to save actually have things plotted on them...

假设您没有num在任何图形构造函数中手动指定(因此您所有的图形编号都是连续的)并且您想要保存的所有图形实际上都绘制了内容......

import matplotlib.pyplot as plt
plot_some_stuff()
# find all figures
figures = []
for i in range(maximum_number_of_possible_figures):
    fig = plt.figure(i)
    if fig.axes:
        figures.append(fig)
    else:
        break

Has the side effect of creating a new blank figure, but better if you don't want to rely on an unsupported interface

具有创建新的空白图形的副作用,但如果您不想依赖不受支持的接口则更好

回答by Wesley Baugh

I tend to name my figures using strings rather than using the default (and non-descriptive) integer. Here is a way to retrieve that name and save your figures with a descriptive filename:

我倾向于使用字符串而不是使用默认(和非描述性)整数来命名我的数字。这是一种检索该名称并使用描述性文件名保存图形的方法:

import matplotlib.pyplot as plt
figures = []
figures.append(plt.figure(num='map'))
# Make a bunch of figures ...
assert figures[0].get_label() == 'map'

for figure in figures:
    figure.savefig('{0}.png'.format(figure.get_label()))

回答by Matti Pastell

Pyplot has get_fignumsmethod that returns a list of figure numbers. This should do what you want:

Pyplot 有get_fignums方法,它返回一个数字列表。这应该做你想做的:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(100)
y = -x

plt.figure()
plt.plot(x)
plt.figure()
plt.plot(y)

for i in plt.get_fignums():
    plt.figure(i)
    plt.savefig('figure%d.png' % i)

回答by krollspell

The following one-liner retrieves the list of existing figures:

以下单行检索现有数字列表:

import matplotlib.pyplot as plt
figs = list(map(plt.figure, plt.get_fignums()))