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
1.0k views
in Technique[技术] by (71.8m points)

python - Saving Matplotlib graphs to image as full screen

I'm building a small graphing utility using Pandas and MatPlotLib to parse data and output graphs from a machine at work.

When I output the graph using

plt.show()

I end up with an unclear image that has legends and labels crowding each other out like so.

Sample Bad Image However, expanding the window to full-screen resolves my problem, repositioning everything in a way that allows the graph to be visible.

I then save the graph to a .png like so

plt.savefig('sampleFileName.png')

But when it saves to the image, the full-screen, correct version of the plot isn't saved, but instead the faulty default version.

How can I save the full-screen plt.show() of the plot to .png?

I hope I'm not too confusing.

Thank you for your help!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The method you use to maximise the window size depends on which matplotlib backend you are using. Please see the following example for the 3 most common backends:

import matplotlib.pyplot as plt

plt.figure()
plt.plot([1,2], [1,2])

# Option 1
# QT backend
manager = plt.get_current_fig_manager()
manager.window.showMaximized()

# Option 2
# TkAgg backend
manager = plt.get_current_fig_manager()
manager.resize(*manager.window.maxsize())

# Option 3
# WX backend
manager = plt.get_current_fig_manager()
manager.frame.Maximize(True)

plt.show()
plt.savefig('sampleFileName.png')

You can determine which backend you are using with the command matplotlib.get_backend(). When you save the maximized version of the figure it will save a larger image as desired.


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

...