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.
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:
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.
['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