MatplotLib 'saveFig()' Fullscreen MatplotLib 'saveFig()' Fullscreen python python

MatplotLib 'saveFig()' Fullscreen


Coming from MATLAB, it is not intuitive that your displayed figure does not have to affect the saved one in terms of dimensions, etc. Each one is handled by a different backend, and you can modify the dpi and size_inches as you choose.

Increasing the DPI is definitely going to help you get a large figure, especially with a format like PNG, which does not know about the size in inches. However, it will not help you scale the text relative to the figure itself.

To do that, you will have to use the object oriented API, specifically, figure.set_size_inches, which I don't think has an equivalent in plt. Replace

plt.savefig(figName, dpi=500)

with

fig = plt.gcf()fig.set_size_inches((8.5, 11), forward=False)fig.savefig(figName, dpi=500)

The size 8.5, 11 is the width and height of the standard paper size in the US, respectively. You can set it to whatever you want. For example, you can use your screen size, but in that case be sure to get the DPI right as well.


Just to add some context on @Mad Physicist answer. If someone is trying to use it with a new version of matplotlib, you will get an AttributeError: 'Figure' object has no attribute 'save'. You also need to be careful about when you call plt.show() otherwise you will get a blank image. You need to update the code as follow:

# Create a plotplt.barh(range(top), imp, align='center')plt.yticks(range(top), names)# Get the current figure like in MATLABfig = plt.gcf()plt.show() # show it here (important, if done before you will get blank picture)fig.set_size_inches((8.5, 11), forward=False)fig.savefig(figName, dpi=500) # Change is over here

Hope it helps!