将 matplotlib 子图图形保存到图像文件
Saving matplotlib subplot figure to image file
我是 matplotlib
的新手,一瘸一拐地前进。也就是说,我还没有找到这个问题的明显答案。
我有一个散点图,我想按组着色,它看起来像 plotting via a loop was the way to roll。
这是我的可重现示例,基于上面的第一个 link:
import matplotlib.pyplot as plt
import pandas as pd
from pydataset import data
df = data('mtcars').iloc[0:10]
df['car'] = df.index
fig, ax = plt.subplots(1)
plt.figure(figsize=(12, 9))
for ind in df.index:
ax.scatter(df.loc[ind, 'wt'], df.loc[ind, 'mpg'], label=ind)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2)
# plt.show()
# plt.savefig('file.png')
取消注释 plt.show()
会得到我想要的结果:
找了一圈,好像plt.savefig()
是保存文件的方法;如果我重新注释掉 plt.show()
和 运行 plt.savefig()
,我会得到一张空白的白色图片。 This question, suggests this is cause by calling show()
before savefig()
, but I have it entirely commented out. Another question 有评论建议我可以直接保存 ax
对象,但这切断了我的图例:
同样的问题有一个替代方案,它使用 fig.savefig()
代替。我得到了同样的切碎图例。
this question 似乎相关,但我没有直接绘制 DataFrame
,所以我不确定如何应用答案(其中 dtf
是 pd.DataFrame
他们在密谋):
plot = dtf.plot()
fig = plot.get_figure()
fig.savefig("output.png")
感谢您的任何建议。
编辑: 测试下面的建议以尝试 tight_layout()
,我 运行 这个仍然得到一个空白的白色图像文件:
fig, ax = plt.subplots(1)
plt.figure(figsize=(12, 9))
for ind in df.index:
ax.scatter(df.loc[ind, 'wt'], df.loc[ind, 'mpg'], label=ind)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2)
fig.tight_layout()
plt.savefig('test.png')
删除行plt.figure(figsize=(12, 9))
,它将按预期工作。 IE。在 show
.
之前调用 savefig
问题是正在保存的图形是由plt.figure()
创建的,而所有数据都绘制到在此之前创建的ax
(并且在不同的图形中,这是不是被拯救的人)。
要保存包含图例的图形,请使用 bbox_inches="tight"
选项
plt.savefig('test.png', bbox_inches="tight")
当然直接保存图形对象也是可以的,
fig.savefig('test.png', bbox_inches="tight")
要更深入地了解如何将图例移出情节,请参阅 this answer。
@ImportanceOfBeingErnest 的答案的附加加法,当 bbox_inches='tight'
、'pad_inches=0.1'
可能需要设置为更大的值。
我是 matplotlib
的新手,一瘸一拐地前进。也就是说,我还没有找到这个问题的明显答案。
我有一个散点图,我想按组着色,它看起来像 plotting via a loop was the way to roll。
这是我的可重现示例,基于上面的第一个 link:
import matplotlib.pyplot as plt
import pandas as pd
from pydataset import data
df = data('mtcars').iloc[0:10]
df['car'] = df.index
fig, ax = plt.subplots(1)
plt.figure(figsize=(12, 9))
for ind in df.index:
ax.scatter(df.loc[ind, 'wt'], df.loc[ind, 'mpg'], label=ind)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2)
# plt.show()
# plt.savefig('file.png')
取消注释 plt.show()
会得到我想要的结果:
找了一圈,好像plt.savefig()
是保存文件的方法;如果我重新注释掉 plt.show()
和 运行 plt.savefig()
,我会得到一张空白的白色图片。 This question, suggests this is cause by calling show()
before savefig()
, but I have it entirely commented out. Another question 有评论建议我可以直接保存 ax
对象,但这切断了我的图例:
同样的问题有一个替代方案,它使用 fig.savefig()
代替。我得到了同样的切碎图例。
this question 似乎相关,但我没有直接绘制 DataFrame
,所以我不确定如何应用答案(其中 dtf
是 pd.DataFrame
他们在密谋):
plot = dtf.plot()
fig = plot.get_figure()
fig.savefig("output.png")
感谢您的任何建议。
编辑: 测试下面的建议以尝试 tight_layout()
,我 运行 这个仍然得到一个空白的白色图像文件:
fig, ax = plt.subplots(1)
plt.figure(figsize=(12, 9))
for ind in df.index:
ax.scatter(df.loc[ind, 'wt'], df.loc[ind, 'mpg'], label=ind)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2)
fig.tight_layout()
plt.savefig('test.png')
删除行plt.figure(figsize=(12, 9))
,它将按预期工作。 IE。在 show
.
savefig
问题是正在保存的图形是由plt.figure()
创建的,而所有数据都绘制到在此之前创建的ax
(并且在不同的图形中,这是不是被拯救的人)。
要保存包含图例的图形,请使用 bbox_inches="tight"
选项
plt.savefig('test.png', bbox_inches="tight")
当然直接保存图形对象也是可以的,
fig.savefig('test.png', bbox_inches="tight")
要更深入地了解如何将图例移出情节,请参阅 this answer。
@ImportanceOfBeingErnest 的答案的附加加法,当 bbox_inches='tight'
、'pad_inches=0.1'
可能需要设置为更大的值。