In this short tutorial let’s explore some examples about using figure and figure size in Python when working with the matplotlib library.
You might want to change the size of a figure after it is created or maybe even before creating it, so let’s check some options so you can start working with the plot size.
Example 1:
figure tells you the call signature:
from matplotlib.pyplot import figure
figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
figure(figsize=(1,1)) would create an inch-by-inch image, which would be 80-by-80 pixels unless you also give a different dpi argument.
Example 2:
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 forward=True
fig.set_size_inches(18.5, 10.5, forward=True)
If you want to continue learning about matplotlib, take a look at the below resources:
Applied Data Science with Python Specialization
References:
Other Dev posts:
Discussion (0)