Python Plt.show 显示完整图形,但 savefig 正在裁剪图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37427362/
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
Plt.show shows full graph but savefig is cropping the image
提问by Joss Kirk
My code is succesfully saving images to file, but it is cropping important details from the right hand side. Answersexist for fixing this problem when it arises for plt.show
, but it is the savefig
command that is incorrectly producing the graph in this example. How can this be fixed?
我的代码成功地将图像保存到文件中,但它正在从右侧裁剪重要的细节。当 出现此问题时,存在解决此问题的答案plt.show
,但savefig
在此示例中,错误生成图形的是命令。如何解决这个问题?
The relevant sample of my code:
我的代码的相关示例:
import glob
import os
for file in glob.glob("*.oax"):
try:
spc_file = open(file, 'r').read()
newName = file[6:8] + '-' + file[4:6] + '-' + file[0:4] + ' ' + file[8:12] + ' UTC (Observed) - No Sea Breeze Day'
plt.title(newName, fontsize=12, loc='left')
plt.savefig('X:/' + newName + '.png')
plt.show()
except Exception:
pass
And the images (top is plt.show
and bottom is file produced from savefig
:
和图像(顶部是plt.show
和底部是从产生的文件savefig
:
回答by Serenity
You may try
你可以试试
plt.savefig('X:/' + newName + '.png', bbox_inches='tight')
Or you may define figure size like
或者您可以定义图形大小,例如
fig = plt.figure(figsize=(9, 11))
...
plt.savefig(filename, bbox_inches = 'tight')