繁体   English   中英

如何从matplotlib.figure.Figure的列表中保存matplotlib图?

[英]How to save a matplotlib figure from a list of matplotlib.figure.Figure's?

创建matplotlib图形后,是否可以保存它? 例如:

import matplotlib.pyplot as plt
import numpy
normal = numpy.random.normal(0, 1, 100)
df = pandas.DataFrame(normal.reshape((10, 10)))

figs = []

for i in df.keys():
    fig = plt.figure()
    plt.plot(df.index, df[i])
    figs.append(fig)

print(figs)

产生列表中的数字:

[<matplotlib.figure.Figure object at 0x7f8f158f1b00>, <matplotlib.figure.Figure object at 0x7f8f100d6128>, <matplotlib.figure.Figure object at 0x7f8f10081da0>, <matplotlib.figure.Figure object at 0x7f8f0a0354a8>, <matplotlib.figure.Figure object at 0x7f8f09fbd470>, <matplotlib.figure.Figure object at 0x7f8f09f18b00>, <matplotlib.figure.Figure object at 0x7f8f09f12d68>, <matplotlib.figure.Figure object at 0x7f8f09e88860>, <matplotlib.figure.Figure object at 0x7f8f09ebbc18>, <matplotlib.figure.Figure object at 0x7f8f09d6e198>]

我不想在创建后直接显式保存该图,而是可以选择以后再做(也许从另一个类中)。 理想的做法是将fig传递给plt.savefig ,但是似乎没有可用于此类使用的插槽。

澄清。 我想要做:

fname = r'/path/to/file.jpg'
plt.savefig(fname, fig=figs[2])

这有可能吗?

只需使用fig.savefig() 这是您的情况的代码片段:

for idx, fig in enumerate(figs):
    fname = '{}_tmp.jpg'.format(idx)
    fig.savefig(fname)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM