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
Let’s look at some examples of changing the figure size in maplotlib:
Change the figure size using
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:
Highlighted programs for you
University of Maryland Global Campus
University of Maryland Global Campus
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()
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()
For the above plot, we first set the
(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()
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
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()
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()
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