Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

python - Plt.show shows full graph but savefig is cropping the image

My code is succesfully saving images to file, but it is cropping important details from the right hand side. Answers exist 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?

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:

Image when shown with plt.show Image when saved to file


See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...