Change Font Size of elements in a Matplotlib plot

In this tutorial we’ll look at how to change the font size of text elements in a matplotlib plot.

You can change the global font size in matplotlib using rcparams. You can also change the font size of individual components such as tick labels, axes labels, axes titles, etc. The following is the syntax:

import matplotlib.pyplot as plt
plt.rcParams.update({'font.size':20})

The above syntax changes the overall font size in matplotlib plots to 20. Note that, the default font size is 10. You can also change the font size of specific components (see the examples below)

Let’s look at some of the use cases of changing font size in matplotlib. First, we’ll create a simple matplotlib line plot and see the default font size.

import matplotlib.pyplot as plt
plt.rcdefaults()

# number of employees of A
emp_count = [3, 20, 50, 200, 350, 400]
year = [2014, 2015, 2016, 2017, 2018, 2019]

# plot a line chart
plt.plot(year, emp_count, 'o-g')
# set axis titles
plt.xlabel("Year")
plt.ylabel("Employees")
# set chart title
plt.title("Employee Growth at A")
plt.show()

Output:

Line plot in matplotlib with default font size.

The above code creates a line plot with text using the default font size.

Let’s change the overall font size of the above plot. Since by default it is 10, we will increase that to 15 to see how the plot appears with a higher font size.

# update the overall font size
plt.rcParams.update({'font.size':15})

# plot a line chart
plt.plot(year, emp_count, 'o-g')
# set axis titles
plt.xlabel("Year")
plt.ylabel("Employees")
# set chart title
plt.title("Employee Growth at A")
plt.show()

Output:

Line plot with a higher overall font size.

You can see that the overall font size of the line chart is now considerably bigger. The tick labels, axis titles, plot title all are comparatively bigger than in the default plot.

You can also set the sizes of individual elements in a matplotlib plot instead of changing the font size of the entire plot. To change the font size of only the tick labels for both the axes:

# reset the plot configurations to default
plt.rcdefaults()

# change the fontsize of the xtick and ytick labels
plt.rc('xtick', labelsize=15)
plt.rc('ytick', labelsize=15)

# plot a line chart
plt.plot(year, emp_count, 'o-g')
# set axis titles
plt.xlabel("Year")
plt.ylabel("Employees")
# set chart title
plt.title("Employee Growth at A")
plt.show()

Output:

Line plot with a higher font size of the axes tick labels

Note that first, we’re using the rcdefaults() function to reset the matplotlib configurations to their defaults, and then we’re customizing our plot. You can see that the labels on the x-axis (2014, 2015, …) and the y-axis (0, 100, …) have a comparatively larger font size.

Similarly, you can also change the size of the axes labels specifically without changing other font sizes.

# reset the plot configurations to default
plt.rcdefaults()

# change the fontsize of axes lables
plt.rc('axes', labelsize=15)

# plot a line chart
plt.plot(year, emp_count, 'o-g')
# set axis titles
plt.xlabel("Year")
plt.ylabel("Employees")
# set chart title
plt.title("Employee Growth at A")
plt.show()

Output:

Plot with the axis labels having a larger font size.

You can see that the axis labels “Employees” and “Year” have a larger font size compared to other text components of the plot.

You can also change the size of the axes title specifically without changing other font sizes.

# reset the plot configurations to default
plt.rcdefaults()

# change the fontsize of axes title
plt.rc('axes', titlesize=20)

# plot a line chart
plt.plot(year, emp_count, 'o-g')
# set axis titles
plt.xlabel("Year")
plt.ylabel("Employees")
# set chart title
plt.title("Employee Growth at A")
plt.show()

Output:

Plot with larger axes title

You can see that the axes title is quite large compared to the axes labels and the tick labels.

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.