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.
Plot a line using two lists. Return the figure manager of the current figure. To toggle full screen image, use full_screen_toggle() method.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With