Matplotlib Figsize | Change the Size of Graph using Figsize

Have you recently started working on graphs and plots using the matplotlib library and don’t know how to change the size of the figures according to your will? Don’t worry. In this article, we will help you manage the size of the plots as you like. If you have not explored the world of matplotlib until now, you can start from here. So, let us learn how to use the matplotlib figsize attribute to adjust the size of the graph.

Matplotlib Figsize is a method used to change the dimension of your matplotlib window. Currently, the window is generated of 6.4×4.8 inches by default. Using this module, you can change it at any size.

What is Figsize in Python?

Matplotlib Figsize is a method from the pyplot class which allows you to change the dimensions of the graph. As every dimension in generated graphs is adjusted by the library, it can be quite difficult to visualize data in a proper format. As a result, the figsize method is very useful to customize the dimensions as well as layouts of the graphs.

We can make the figure taller in size, broader by changing the size in inches.  

Syntax of Matplotlib Figsize

To make a plot or a graph using matplotlib, we first have to install it in our system using pip install matplotlib. 

Also, figsize is an attribute of figure() class of pyplot submodule of matplotlib library. So, the syntax is something like this- 



  1. Width – Here, we have to input the width in inches. The default width is 6. To broaden the plot, set the width greater than 1. And to make the graph less broad, set the width less than 6. 
  2. Height – Here, we have to input the height of the graph. The default value is 4. To increase the length, set the height greater than 4, and to decrease the height set the height less than 4. 

What is default Figsize Matplotlib?

In Matplotlib all the diagrams are created at a default size of 6.4 x 4.8 inches. This size can be changed by using the Figsize method of the respective figure. This parameter is governed under the rcParams attribute of the figure. By using Figsize, you can change both of these values.

The default figure size values are stored as a list of two float objects. For example, rcParams[“figure. figsize”] will be equal to [6.4, 4.8] in a programmatic way.

What is rcParams figure Figsize?

rcParams is a dictionary attribute in Matplotlib figure class that allows you to customize the look of your graph. This attribute is responsible for carrying data of figure size, figure DPI, figure facecolor, and figure edgecolor. All of these are initialized to default values of [6.4, 4.8], 100, ‘w’, and ‘w’ respectively.

Figsize is a key in rcParams which changes the figure size of your data visualization. As of now, you cannot change the rcParams directly, but you can use different methods like figsize to do it.

What are Matplotlib Figsize Units?

Matplotlib have general way of keeping all the distances in inches. For figsize too, the units are measured in inches by default. if you want a to use a different unit for the figsize, you can achieve it by creating a converting function. Following example explains a way to initialize the Figsize in centimeters (cm) –

Code –

import matplotlib.pyplot as plt
import numpy as np

def cm_to_inch(value):
    return value/2.54

x = np.arange(0, 10, 0.1)
y = np.sin(x)

plt.figure(figsize=(cm_to_inch(25), cm_to_inch(15)))
plt.plot(x, y)

Output –

Matplotlib Figsize Output

Explanation –

We first start by importing the matplotlib library. Then we create an array of x-axis whose value ranges from 0 to 10 at 0.1 intervals. np.sin() function generates a sine wave function values. The function cm_to_inch() converts all the values of centimeters to inches and initializes the dimensions of the figure.

Changing the Height and Width of the Graph

Both height and width can be changed by the figure.figsize() method. Following example demonstrates the figures with default size and after them are the customized figures of different dimensions.

Graph with Default Size 

Let us first know how to make a graph without setting any specific size and see if it is our desirable size or not.

We will create a simple plot by creating our own data. 

import matplotlib.pyplot as plt
y=[i**2 for i in x]
plt.title("Plot of Default Size")
matplotlib figsize

Changing the Height using Matplotlib Figsize

Suppose we have a pandas data frame that contains information about some sports and how many people play those sports. Something like this-

We want to make a bar chart from it, let us first make a graph with the default size. I have made that data frame in the form of an array to make the task easy.

# import the pyplot submodule of matplotlib library
# and rename it as plt
import matplotlib.pyplot as plt

# name of the sports
sports_name=['Aquatics', 'Athletics', 'Rowing', 'Gymnastics', 'Fencing', 'Football','Hockey', 'Wrestling', 'Shooting', 'Sailing', 'Cycling','Canoe / Kayak', 'Basketball', 'Volleyball', 'Equestrian', 'Handball','Boxing', 'Weightlifting', 'Judo', 'Baseball', 'Archery', 'Tennis','Rugby', 'Softball', 'Modern Pentathlon', 'Table Tennis', 'Badminton','Tug of War', 'Taekwondo', 'Polo', 'Lacrosse', 'Golf', 'Ice Hockey','Skating', 'Cricket', 'Triathlon', 'Rackets', 'Croquet','Water Motorsports', 'Basque Pelota', 'Jeu de paume', 'Roque']

# These are people which play the respective sport
people_playing_the_sports=[3828, 3448, 2523, 2214, 1547, 1387, 1325,140,105,1061,1025,1002,940,910,894,886,842,548,435,335,305,272,192,180,174,120,120,94,80,66,59,30,27,27,24,18,10,8,5,4,3,3]

# To create a bar graph use bar function of pyplot,people_playing_the_sports)
# Rotate the name of sports by 90 degree in x-axis
# show the graph
matplotlib figsize

We can see that due to a lack of space, the graph is too untidy. For a better-looking graph, we need to change the width of the graph. Let us see how we will do that.

import matplotlib.pyplot as plt
people_playing_the_sports=[3828, 3448, 2523, 2214, 1547, 1387, 1325,1140,105,1061,1025,1002,940,910,894,886,842,548,435,335,305,272,192,180,174,120,120,94,80,66,59,30,27,27,24,18,10,8,5,4,3,3]

sports_name=['Aquatics', 'Athletics', 'Rowing', 'Gymnastics', 'Fencing', 'Football','Hockey', 'Wrestling', 'Shooting', 'Sailing', 'Cycling','Canoe / Kayak', 'Basketball', 'Volleyball', 'Equestrian', 'Handball','Boxing', 'Weightlifting', 'Judo', 'Baseball', 'Archery', 'Tennis','Rugby', 'Softball', 'Modern Pentathlon', 'Table Tennis', 'Badminton','Tug of War', 'Taekwondo', 'Polo', 'Lacrosse', 'Golf', 'Ice Hockey',
'Skating', 'Cricket', 'Triathlon', 'Rackets', 'Croquet',
'Water Motorsports', 'Basque Pelota', 'Jeu de paume', 'Roque']

# Increase the width

Isn’t it a lot better now? Just changing the size of the graph has made such a huge impact and now we can understand the graph a lot better.

matplotlib figsize

Changing the Height of the Graph using Matplotlib Figsize

Suppose we have created the same bar chart horizontally, the default height would look untidy, right? Let us see how we can increase the height of the graph.

import matplotlib.pyplot as plt

people_playing_the_sports=[3828, 3448, 2523, 2214, 1547, 1387, 1325,1140,1105,1061,1025,1002,940,910,894,886,842,548,435,335,305,272,192,180,174,120,120,94,80,66,59,30,27,

sports_name=['Aquatics', 'Athletics', 'Rowing', 'Gymnastics', 'Fencing', 'Football','Hockey', 'Wrestling', 'Shooting', 'Sailing', 'Cycling','Canoe / Kayak', 'Basketball', 'Volleyball', 'Equestrian', 'Handball','Boxing', 'Weightlifting', 'Judo', 'Baseball', 'Archery', 'Tennis','Rugby', 'Softball', 'Modern Pentathlon', 'Table Tennis', 'Badminton','Tug of War', 'Taekwondo', 'Polo', 'Lacrosse', 'Golf', 'Ice Hockey',
'Skating', 'Cricket', 'Triathlon', 'Rackets', 'Croquet',
'Water Motorsports', 'Basque Pelota', 'Jeu de paume', 'Roque']

# Increase the width and the height
matplotlib figsize

Explanation –

Firstly, we start by importing the matplotlib library. then we declared the players playing the sports and sports name in the list. Then by initializing the matplotlib figure by using a parameter of figsize, we can create a bigger-sized output. In our case, we’ve initialized it to 15 x 10 inches. As you can observe from the output, the data is widely spaced and it’s larger than the default dimensions.

Changing the figsize of the Matplotlib Subplots

We can also create subplots using matplotlib. In subplots, we create multiple subplots in a single figure. The more the number of subplots in a figure, the size of the subplot keeps changing. We can change the size of the figure and whatever size we give will be divided into subplots.

import matplotlib.pyplot as plt
# make subplots with 2 rows and 1 column.
# We If there were 3 rows, we would have done-fig, (ax1,ax2,ax3)
fig, (ax1,ax2) = plt.subplots(nrows=2,ncols=1,figsize=(6,8))
y=[i*i for i in range(10)]
#plotting for 1st subplot
#plotting for 2nd subplot,y)

Matplotlib Figsize Not Working Error

Sometimes, we may get an error- ‘matplotlib figsize not working’. This happens when we first plot the graph and then set the figsize.

import matplotlib.pyplot as plt
technologies=['Data Science', 'Cyber Security', 'Web Development', 'Android Development', 'Data Analyst'] 

# Wrong way-,number_of_students)

# Correct way

This is the common mistake that new programmers can do in using this method. Always remember that you can only set the figsize before the initialization of the graph. If the graph is already initialized, you cannot change the dimensions of the figure. Make sure that you place plt.figure() before your to avoid this error.

How to Set the Figure Size in Pixels

We can also set the figure size of the graph or chart according to the monitor, we can use dpi of our monitor to do so. You can detect the dpi of your monitor by search on the internet. There are multiple tools to do so.

# the dpi of my monitor is 120
# make a figure with the follwing figsize
plt.figure(figsize=(400/my_dpi, 300/my_dpi), dpi=my_dpi)[1,2,3,4,5],[5,4,3,2,1],align="center")

Must Read:


We have seen that how changing the width and height can result in making the graph a lot better to understand and create insights. We can do this using the matplotlib figsize attribute of figure() function of the matplotlib library.

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

3 2 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
2 days ago

Your work is Superb mate to teach people, thanks for sharing it,