Saving Matplotlib graphs to image as full screen
Solution 1:
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.
Solution 2:
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.
Solution 3:
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)
Solution 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()
Solution 5:
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()