Matplotlib is a handy visualization library in python that offers a number of plotting options and customizations. It may happen that at times you’d want to change the size of the plots as per your needs. In this tutorial, we’ll look at how to change the size of figures plotted with matplotlib.
How to change the size of figures drawn with matplotlib?
To have the plots of a specific size in matplotlib, you can pass the figure size to the figsize
parameter of matplotlib pyplot’s figure()
function or specify the default figure size via rcParams
. The following is the syntax:
import matplotlib.pyplot as plt
plt.figure(figsize=(width,height))
Here, we pass the desired dimensions of the plot as a (width,height)
tuple to figsize
. Note that the width and height should be in inches. So if you want your plot to be 8 inches wide and 6 inches high, pass (8,6) to figsize
. The default size of a plot in matplotlib is (6.4,4.8)
Examples
Let’s look at some examples of changing the figure size in maplotlib:
Change the figure size using figure()
First, let’s plot a simple line chart with maplotlib without explicitly setting the figure size. Here’s a line chart showing the employee count growth at a company over the last few years:
import matplotlib.pyplot as plt
# number of employees of ABC
emp_count = [3, 20, 50, 200, 350, 400]
year = [2014, 2015, 2016, 2017, 2018, 2019]
# plot a line chart
plt.plot(year, emp_count)
plt.xlabel("Year")
plt.ylabel("Count")
plt.title("Employee count at ABC")
plt.show()
Output:
This is the default figure size we get on plotting with matplotlib. Now let’s make this plot bigger.
# change the figure size
plt.figure(figsize=(8,6))
# plot a line chart
plt.plot(year, emp_count)
plt.xlabel("Year")
plt.ylabel("Count")
plt.title("Employee count at ABC")
plt.show()
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
For the above plot, we first set the figsize
to (8,6)
and then proceeded to plot the line chart. You can see that this figure is indeed larger than the previous one. Let’s see what happens if we attempt to plot the same figure without setting the figsize again.
# plot a line chart
plt.plot(year, emp_count)
plt.xlabel("Year")
plt.ylabel("Count")
plt.title("Employee count at ABC")
plt.show()
Output:
Here we get the plot with the default figure size, smaller than the one we got in the above example where we specified the size. This means that you’ll have to specify the figsize each time you want a plot (with a size other than default) using this method.
Set a default figsize in matplotlib using rcParams
Let’s say your working in a Jupyter Notebook and want to set a default figsize without having to specify it each time you plot. For this, use rcParams
which is used for handling default Matplotlib values.
plt.rcParams['figure.figsize'] = [10, 8]
# plot a line chart
plt.plot(year, emp_count)
plt.xlabel("Year")
plt.ylabel("Count")
plt.title("Employee count at ABC")
plt.show()
Output:
The size configuration set with rcParams
remains consistent for other plots as well. For instance, if you plot a bar chart of the same values without setting the figure size:
# plot a bar chart
plt.bar(year, emp_count)
plt.xlabel("Year")
plt.ylabel("Employees")
plt.title("Employee count at ABC")
plt.show()
Output:
The resulting bar chart retains the figure size settings specified with rcParams
in the previous example.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having matplotlib version 3.2.2
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on matplotlib –
- Change Background Color of Plot in Matplotlib
- Change Font Size of elements in a Matplotlib plot
- Matplotlib – Save Plot as a File
- Change Size of Figures in Matplotlib
- Plot a Bar Chart using Matplotlib
- Plot a Pie Chart with Matplotlib
- Plot Histogram in Python using Matplotlib
- Create a Scatter Plot in Python with Matplotlib
- Plot a Line Chart in Python with Matplotlib
- Save Matplotlib Plot with Transparent Background
- Change Font Type in Matplotlib plots