Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Matplotlib graphs to image as full screen

People also ask

How do I save Matplotlib figures with high resolution?

To save the file in pdf format, use savefig() method where the image name is myImagePDF. pdf, format="pdf". We can set the dpi value to get a high-quality image. Using the saving() method, we can save the image with format=”png” and dpi=1200.

How do you make a fullscreen plot in Python?

Plot a line using two lists. Return the figure manager of the current figure. To toggle full screen image, use full_screen_toggle() method.

How do you save a graph as a picture in Python?

Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

How do I save a Matplotlib file as a JPEG?

To save plot figure as JPG or PNG file, call savefig() function on matplotlib. pyplot object. Pass the file name along with extension, as string argument, to savefig() function.


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.


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.


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)

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