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 matplotlib figsize attribute to adjust the size of the graph. 

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() function which is a function of pyplot submodule of matplotlib library. So, the syntax is something like this- 

matplotlib.pyplot.figure(figsize=(float,float)) 

Parameters- 

  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. 

Changing the Height and Width of the Graph

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
x=[1,2,3,4,5,6,7,8,9]
y=[i**2 for i in x]
plt.plot(x,y)
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
plt.bar(sports_name,people_playing_the_sports)
# Rotate the name of sports by 90 degree in x-axis
plt.xticks(rotation=90)
# show the graph 
plt.show()
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
plt.figure(figsize=(15,4))
plt.bar(Sports,people_playing_the_sports)
plt.xticks(rotation=90)
plt.show()

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,
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 and the height
plt.figure(figsize=(15,10))
plt.barh(Sports,people_playing_the_sports)
plt.xticks(rotation=90)
plt.show()
matplotlib figsize

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 the 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
ax1.plot(range(10),y)
#plotting for 2nd subplot
ax2.bar(range(10),y)

Matplotlib figsize not working Error

Sometimes, what 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'] 
number_of_students=[78,49,112,129,59]

# Wrong way-
plt.bar(technologies,number_of_students)
plt.figure(figsize=(10,5))
plt.xticks(rotation=90)
plt.show()


# Correct way
plt.figure(figsize=(10,5))
plt.bar(technologies,number_of_students)
plt.xticks(rotation=90)
plt.show()

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
my_dpi=120
# make a figure with the follwing figsize
plt.figure(figsize=(400/my_dpi, 300/my_dpi), dpi=my_dpi)
plt.bar([1,2,3,4,5],[5,4,3,2,1],align="center")
plt.show()

Must Read:

Conclusion

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!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments