In this tutorial, we’ll look at how to change the font types of texts in matplotlib plots.
How to change Font in Matplotlib?
You can change the font globally for all the plots using rcParams
. (See the syntax and examples below). You can also set the font individually for text components of a matplotlib axes object such as axes labels, title, tick labels, etc. The following is the syntax:
# set the font globally plt.rcParams.update({'font.family':'sans-serif'})
The above syntax assumes matplotlib.pyplot
is imported as plt
.
In matplotlib, the font.family
property can take one of the following values – ‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, and ‘monospace’. Note that, each font family has a default list of font names in decreasing order of priority associated with them. You can also specify which font or order of fonts to use for a particular font family using the below syntax:
# set the font name for a font family plt.rcParams.update({'font.sans-serif':'Helvetica'})
Examples
Let’s look at some examples of setting the font styles in matplotlib plots. First, we’ll create a simple matplotlib line plot and see the default font in the plot.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
import matplotlib.pyplot as plt # reset the plot configurations to default 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 fig, ax = plt.subplots() ax.plot(year, emp_count) # set axis titles ax.set_xlabel("Year") ax.set_ylabel("Employees") # set chart title ax.set_title("Employee Growth at A") plt.show()
Output:

The text in the above plot uses its default font style. You can check the default font family using the following code.
# the current font family print(plt.rcParams['font.family'])
Output:
['sans-serif']
You can print out the list of font names used in the above font family.
# list of fonts in sans-serif plt.rcParams['font.sans-serif']
Output:
['DejaVu Sans', 'Bitstream Vera Sans', 'Computer Modern Sans Serif', 'Lucida Grande', 'Verdana', 'Geneva', 'Lucid', 'Arial', 'Helvetica', 'Avant Garde', 'sans-serif']
Let’s go ahead and change the font used in the above plot. There are multiple ways to change the font style of text in matplotlib plots – You can add a default font for all the plots using rcParams or you can set a font style individually for text components of your axes objects.
1. Change the default font for all plots using rcParams
In matplotlib, you can set the default configurations of a number of plot features using rcParams
. Let’s change the default font family to “fantasy” and see how the above plot appears.
# change the default font family plt.rcParams.update({'font.family':'fantasy'}) # plot a line chart fig, ax = plt.subplots() ax.plot(year, emp_count) # set axis titles ax.set_xlabel("Year") ax.set_ylabel("Employees") # set chart title ax.set_title("Employee Growth at A") plt.show()
Output:

You can see in the above plot that the font of all the text elements is now from a different font family (“fantasy”).
Since we have updated the default font style, all the following plots will have the same font family(“fantasy” in our case). Let’s create a different plot to see this in action.
# plot a line chart fig, ax = plt.subplots() ax.scatter(year, emp_count) # set axis titles ax.set_xlabel("Year") ax.set_ylabel("Employees") # set chart title ax.set_title("Employee Growth at A") plt.show()
Output:

You can see that the above scatter plot also has the text with the new default font family.
2. Change font of individual text component of a plot
Alternatively, you can also change the font of individual text components such as axes title, axes labels, tick labels, etc of an axes object without changing the global settings. For example, let’s change the font of our axes title in the line chart.
# reset the plot configurations to default plt.rcdefaults() # plot a line chart fig, ax = plt.subplots() ax.plot(year, emp_count) # set axis titles ax.set_xlabel("Year") ax.set_ylabel("Employees") # set chart title ax.set_title("Employee Growth at A", fontname="Comic Sans MS") plt.show()
Output:

Note that first, we used the rcdefaults() function to reset the matplotlib configurations to their defaults, and then went ahead to update our font. Note that except for the axes title (which has font “Comic Sans MS”) all other text components have the default font style.
You can make similar changes to other text components of the plot as well and this way you do not alter the default configurations.
For more on customizing matplotlib default parameters, refer to this guide.
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