DEV Community

loading...
Cover image for How do you change the size of figures drawn with matplotlib?

How do you change the size of figures drawn with matplotlib?

courseprobe profile image Course Probe ・2 min read

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')
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

To propagate the size change to an existing gui window add forward=True

fig.set_size_inches(18.5, 10.5, forward=True)
Enter fullscreen mode Exit fullscreen mode

If you want to continue learning about matplotlib, take a look at the below resources:

Matplotlib Tutorial

Applied Data Science with Python Specialization

References:


Other Dev posts:

Discussion

pic
Editor guide