Change Size of Figures in Python Matplotlib & seaborn (2 Examples)

 

Hi! This tutorial will demonstrate how to adjust the size of figures in Matplotlib and seaborn in Python.

Here is an overview:

Let’s get into the Python code!

 

Install & Import Matplotlib & seaborn

To install and import Matplotlib and seaborn, run the lines of code below in your preferred Python programming IDE or code editor:

# install Matplotlib & seaborn
pip install matplotlib seaborn
 
# import Matplotlib & seaborn
import matplotlib.pyplot as plt
 
import seaborn as sns

Now that we have installed and imported Matplotlib and seaborn into our Python programming environment, we can now build visualizations.

However, we need dataset to visualize.
 

Create Example Dataset

Here, we will create the example dataset that we will use to demonstrate how to modify the size of a figure in Matplotlib and seaborn.

We will load the popular iris dataset that is shipped with the Python seaborn library. To load and preview the first 10 rows of the DataFrame, run the lines of code below:

df = sns.load_dataset("iris")
 
df.head(10)
 
#sepal_length	sepal_width	petal_length	petal_width	species
#0	  5.1	        3.5	         1.4	        0.2	 setosa
#1	  4.9	        3.0	         1.4	        0.2	 setosa
#2	  4.7	        3.2	         1.3	        0.2	 setosa
#3	  4.6	        3.1	         1.5	        0.2	 setosa
#4	  5.0	        3.6	         1.4	        0.2	 setosa
#5	  5.4	        3.9	         1.7	        0.4	 setosa
#6	  4.6	        3.4	         1.4	        0.3	 setosa
#7	  5.0	        3.4	         1.5	        0.2	 setosa
#8	  4.4	        2.9	         1.4	        0.2	 setosa
#9	  4.9	        3.1	         1.5	        0.1	 setosa

With the dataset now loaded, we can go on to show how to change the size of a figure in Matplotlib and seaborn.
 

Example 1: Adjust Size of Figure in Matplotlib

In this example, we will build a scatter plot and then demonstrate how to change its size:

plt.scatter(df["sepal_length"], df["petal_width"])
 
plt.xlabel("Sepal Length")
 
plt.ylabel("Petal Width")
 
plt.show()

 

Scatter plot with default size

 

In the example above, we built the scatter plot using the plt.scatter() method wherein we passed the sepal_length column of df to the x axis and the petal_width column of df to the y axis.

We then used the plt.xlabel() and plt.ylabel() methods to label the x and y axis of the plot.

Lastly, we displayed the plot using plt.show().

Now, we will demonstrate how to adjust or modify the size of the plot. There are at least 2 ways you can do this. To demonstrate the first way, run the lines of code below:

plt.figure(figsize=(10,4))
 
plt.scatter(df["sepal_length"], df["petal_width"])
 
plt.xlabel("Sepal Length")
 
plt.ylabel("Petal Width")
 
plt.show()

 

Scatter plot with adjusted dimensions
 

To change the size of the figure, we used the plt.figure() method wherein we passed a tuple containing the width and height of the plot in pixels to the figsize = argument.

Notice that we wrote plt.figure() before the plt.scatter() method. This is to define the dimensions of the plot or figure before it is built. If it is written after plt.scatter(), the plot will be built with its default size.

Another way to change the size of the plot is demonstrated below:

plt.figure().set_figwidth(10)
 
plt.figure().set_figheight(7)
 
plt.scatter(df["sepal_length"], df["petal_width"])
 
plt.xlabel("Sepal Length")
 
plt.ylabel("Petal Width")
 
plt.show()

 

Scatter plot with adjusted size

 

In the above example, we used the set_figwidth() and set_figheight() methods to adjust the width and height of the figure.

Also, the set_figwidth() and set_figheight() should precede the plt.scatter() otherwise, the plot won’t turn out nice.

This is another way to change the size of a figure or plot in Matplotlib.
 

Example 2: Adjust Size of Figure in seaborn

Here, we will build a simple line plot where we will plot the sepal_length column of df on the x axis and the petal_length column of df on the y axis:

sns.lineplot(df, x = "sepal_length", y = "petal_width")
 
plt.xlabel("Sepal Length")
 
plt.ylabel("Petal Width")
 
plt.show()

 

Line plot with default size

 

In the above example, we have built a simple line plot using the sns.lineplot() method wherein we passed the DataFrame and set the x and y axes as sepal_length and petal_width respectively and also labelled the x and y axes using plt.xlabel() and plt.ylabel().

Now, we will demonstrate how to change the size of the plot. Run the lines of code below to do so:

plt.figure(figsize = (10,5))
 
sns.lineplot(df, x = "sepal_length", y = "petal_width")
 
plt.xlabel("Sepal Length")
 
plt.ylabel("Petal Width")
 
plt.show()

 

Line plot with adjusted size

 

As you can see, we used the plt.figure() method to set the size of the plot just as we did in the Matplotlib example above. Here, we set the width to 10 and length to 5.

As in the Matplotlib example, writing plt.figure() after the plot-building method will result in a plot with default dimensions.

Also, since seaborn is based on Matplotlib, you can apply the second method of changing a plot’s size in Matplotlib to your seaborn figure as well.

 

Video, Further Resources & Summary

Do you need more explanations on how to change the size of a figure in Python Matplotlib and seaborn? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to change the size of a figure in Python Matplotlib and seaborn.

 

The YouTube video will be added soon.

 

So, we have demonstrated how to change or set the size of figures in Python Matplotlib and seaborn. Furthermore, you could have a look at some of the other interesting Mattplotlib and seaborn tutorials on Statistics Globe:

This post has shown how to change the size of a figure in Python Matplotlib and seaborn. I hope you found it helpful! In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top