How do you change the size of figures drawn with Matplotlib?
If you've already got the figure created, you can quickly do this:
fig = matplotlib.pyplot.gcf()fig.set_size_inches(18.5, 10.5)fig.savefig('test2png.png', dpi=100)
To propagate the size change to an existing GUI window, add
fig.set_size_inches(18.5, 10.5, forward=True)
There is also this workaround in case you want to change the size without using the figure environment. So in case you are using
plt.plot() for example, you can set a tuple with width and height.
import matplotlib.pyplot as pltplt.rcParams["figure.figsize"] = (20,3)
To reset the global figure size back to default for subsequent plots:
plt.rcParams["figure.figsize"] = plt.rcParamsDefault["figure.figsize"]
Conversion to cm
figsize tuple accepts inches, so if you want to set it in centimetres you have to divide them by 2.54. Have a look at this question.