31

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!

4
  • 1
    Can you share a MCVE please? Or all code, if possible. Commented Sep 6, 2015 at 20:56
  • I don't think it would be too helpful in this case. Sorry, I'm being confusing. I want savefig to save the graph whose picture I posted the way it looks when plt.show() is full-screen. One possible solution may be to change the window size of plt.show(). How could I do that? Commented Sep 6, 2015 at 21:02
  • Possible duplicate of How to maximize a plt.show() window using Python Commented May 2, 2018 at 8:17
  • disagree it is a duplicate, because although the answer is effectively the same, it may not be clear to someone who is either asking a question or searching for a question that what savefig() saves is equivalent to what show() shows Commented Mar 26, 2024 at 7:24

6 Answers 6

54

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.

Sign up to request clarification or add additional context in comments.

11 Comments

QT, apparently! Worked like a charm! :) Many thanks, my friend.
Not quite sure why you want to make your plot bigger than full-screen. You could set the x-lim and y-lim to zoom in on a certain section of your plot. Adjust your code to plt.show() plt.tight_layout() plt.savefig('sampleFileName.png') to make the plot take up more of the maximized window.
I'm using TkAgg backend, this solution shows plot in maximized view, but saves it in default size. Can you help?
You can also look into plt.savefig('figname.png', bbox_inches='tight')
@ElginCahangirov did you found any solution to your issue?
|
16

For everyone, who failed to save the plot in fullscreen using the above solutions, here is what really worked for me:

    figure = plt.gcf()  # get current figure
    figure.set_size_inches(32, 18) # set figure's size manually to your full screen (32x18)
    plt.savefig('filename.png', bbox_inches='tight') # bbox_inches removes extra white spaces

You may also want to play with the dpi (The resolution in dots per inch)

    plt.savefig('filename.png', bbox_inches='tight', dpi=100)

2 Comments

If your on MacOS 10.15.7 with Python 3.7 this is the only solution from above that works :)
This works to save the full screen image automatically
10

As one more option, I think it is also worth looking into

plt.savefig('filename.png', bbox_inches='tight')

This is especially useful if you're doing subplots that has axis labels which look cluttered.

1 Comment

This was exactly what I wanted. Many many thanks!
4

For those that receive errors in the answers above, this has worked for me.

#Show full screen
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

Full example

fig = plt.figure()
fig.imshow(image)
...
plt.figure(fig.number)
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
fig.show()
fig.savefig('figure.png')
mng.full_screen_toggle()

Comments

1

I've found the most common answer on this website. It's a ragbag of all of those answers. It seems that it can be used in all cases without compatibility problems.

Don't hesitate to comment if you have some troubles with this answer.

# Maximise the plotting window
plot_backend = matplotlib.get_backend()
mng = plt.get_current_fig_manager()
if plot_backend == 'TkAgg':
    mng.resize(*mng.window.maxsize())
elif plot_backend == 'wxAgg':
    mng.frame.Maximize(True)
elif plot_backend == 'Qt4Agg':
    mng.window.showMaximized()

Comments

0

There is one more bit I want to add since this is likely one of the first threads people will find when looking for an answer. While the above answers work, you might not want to close all windows manually when you are plotting hundreds of images. On the other hand, skipping plt.show() can cause errors in the plot. Therefore, my process for saving fullscreen images looks like this (using the QT backend):

def plot_fullscreen():
    manager = plt.get_current_fig_manager()
    manager.window.showMaximized()

def your_plotting_function():
    fig, ax = plt.subplots()
    
    # Do your plotting things here
    
    plot_fullscreen()
    gcf = plt.gcf()
    plt.show(block=False)
    plt.pause(0.01)
    gcf.savefig("figure_name.png", bbox_inches='tight')
    plt.close(fig)

Removing plt.pause() will cause it to not properly save in fullscreen. My best guess is that this is related to some things happening in the background as the pause() documentation states:

If there is an active figure, it will be updated and displayed before the pause, and the GUI event loop (if any) will run during the pause

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.