Python 使用 matplotlib.pyplot 保存图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29774707/
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
Save image with matplotlib.pyplot
提问by alpagarou
This is a very simple question but I must misunderstand the use of pyplot and figure or something else. I'm plotting some images and would like to save them instead of just showing them and saving them by hand. So far I've tried:
这是一个非常简单的问题,但我必须误解 pyplot 和 figure 或其他东西的使用。我正在绘制一些图像并想保存它们,而不是仅仅显示它们并手动保存它们。到目前为止,我已经尝试过:
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(d,c1[0:100],'b--',d,c2[0:100],'r--',d,c3[0:100],'g--',figure = fig)
plt.ylabel("concentration")
plt.xlabel("distance")
plt.show()
plt.savefig('./Results/evol_conc_v'+str(vinit)+'a_'+str(a)+'.png')
The file created is blank, but the image showed was good. The existing similar question don't seem to apply.
创建的文件是空白的,但显示的图像很好。现有的类似问题似乎不适用。
采纳答案by lmNt
Get rid of the
摆脱
plt.show()
or put it below the savefig
call.
或者把它放在savefig
电话下面。
Or you do this
或者你这样做
plt.show()
fig.savefig('./Results/evol_conc_v'+str(vinit)+'a_'+str(a)+'.png') # Use fig. here
since you already create a figure object at the beginning.
因为您已经在开始时创建了一个图形对象。