The world’s leading publication for data science, AI, and ML professionals.

How To Change The Size Of Figures In Matplotlib

Discussing how to adjust the size of figures created with matplotlib in Python

Photo by Deon Black on Unsplash
Photo by Deon Black on Unsplash

Introduction

Resizing figures generated through matplotlib in Python is a common task when it comes to visualizing data. In today’s short guide we will discuss a few possible ways for adjusting the size of the generated plots.

Specifically, we will discuss how to do so:

  • Using matplotlib.pyplot.figure()
  • Using set_size_inches()
  • by modifying rcParams['figure.figsize']

Additionally, we will discuss how to resize a figure using a factor/ratio of the existing (default) size.


First, let’s create a figure using some dummy data that we’ll use throughout this article in order to demonstrate a couple of concepts.

import matplotlib.pyplot as plt
x = y = range(1, 10)
plt.plot(x, y)
plt.show()

The figure shown below is generated using the default size, as defined in rcParams['figure.figsize'] .


Using figure

The first option you have is to call [matplotlib.pyplot.figure](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html) that is used to create a new figure or activate an existing one. The method accepts an argument called figsize that is used to specify the width and height of the figure (in inches). Additionally, you can even specify dpi that corresponds to the resolution of the figure in dots-per-inch.

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
figure(figsize=(3, 2), dpi=80)
x = y = range(1, 10)
plt.plot(x, y)
plt.show()

Using set_size_inches

The second option you have is [matplotlib.figure.set_size_inches()](https://matplotlib.org/stable/api/figure_api.html?highlight=figure#matplotlib.figure.Figure.set_size_inches) that is used to set the figure size in inches.

import matplotlib.pyplot as plt
x = y = range(1, 10)
plt.plot(x, y)
plt.gcf().set_size_inches(3, 2)
plt.show()

Modifying rcParams

If you want to modify the size of a figure without using the figure environment, then you can also update matplotlib.rcParams which is an instance of [RcParams](https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.RcParams) for handling default Matplotlib values.

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (3, 2)
x = y = range(1, 10)
plt.plot(x, y)
plt.show()

Note that the above will have effect on every figure generated unless you specify different size for a specific figure. If for any reason you want to set back the default values of this parameter, you can simply use rcParamsDefault as shown below

plt.rcParams['figure.figsize']=plt.rcParamsDefault['figure.figsize']

Resizing a figure using a factor

Now if you wish to resize a figure using a hardcoded factor or ratio in relation say to another figure, then you can do so using the commands below:

figure_size = plt.gcf().get_size_inches()
factor = 0.8
plt.gcf().set_size_inches(factor * figure_size)

Final Thoughts

In today’s short guide we discussed how to resize figures generated using matplotlib library. We explore a few possible options but you should make sure to use the one that suits your needs (e.g. depending on whether you want to specify the size for all the figures and plots, or just a specific on).


Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read.


You may also like

How to Upload Your Python Package to PyPI

What Are Named Tuples in Python

Easter Eggs in Python


Related Articles