29

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!

kmario23
  • 57,311
  • 13
  • 161
  • 150
  • 1
    Can you share a [MCVE](http://stackoverflow.com/help/mcve) please? Or all code, if possible. – Sait Sep 06 '15 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? –  Sep 06 '15 at 21:02
  • Possible duplicate of [How to maximize a plt.show() window using Python](https://stackoverflow.com/questions/12439588/how-to-maximize-a-plt-show-window-using-python) – jdhao May 02 '18 at 08:17

5 Answers5

52

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.

gtlambert
  • 11,711
  • 2
  • 30
  • 48
  • QT, apparently! Worked like a charm! :) Many thanks, my friend. –  Sep 06 '15 at 21:08
  • Is there any way to maximize the plot to bigger than full-screen? –  Sep 08 '15 at 20:41
  • 1
    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. – gtlambert Sep 09 '15 at 08:17
  • Have in mind that `manager.resize(*manager.window.maxsize())` will set the biggest window size available, which is not the expected behviour on multiple screen layouts. – jcrs Jan 13 '17 at 13:45
  • 10
    I'm using TkAgg backend, this solution shows plot in maximized view, but saves it in default size. Can you help? – Elgin Cahangirov Feb 05 '17 at 08:33
  • 2
    You can also look into `plt.savefig('figname.png', bbox_inches='tight')` – kmario23 Feb 21 '17 at 05:24
  • @gtlambert any solution to work around with that? I have the similar issue as Elgin Cahangirov.... it plot in maximized view but saves it in default size. Have since open a new thread https://stackoverflow.com/questions/45867016/trouble-saving-matplotlib-figure-in-max-windows-form?noredirect=1#comment78715434_45867016 but haven gotten any solution yet – SacreD Aug 29 '17 at 10:32
  • 1
    @ElginCahangirov did you found any solution to your issue? – SacreD Aug 29 '17 at 10:34
  • @SacreD sorry, I couldn't remember why I asked this question, but right now I don't know the answer of this question – Elgin Cahangirov Sep 02 '17 at 08:42
  • @SacreD - This is probably a bug introduced in 2.0. I've provided a link to a workaround in the new question you opened. – OmerB Sep 24 '17 at 20:09
  • this is an old question but, where did you find how to maximize the window for those back-ends? I am interested in how, not in just making it work this time. – George Sp Jan 06 '19 at 12:59
12

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)
stepj
  • 151
  • 1
  • 2
  • 8
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.

kmario23
  • 57,311
  • 13
  • 161
  • 150
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()
Burak
  • 2,251
  • 1
  • 16
  • 33
Westly White
  • 543
  • 9
  • 14
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()
Apitronix
  • 305
  • 2
  • 13