Change font in a maplotlib plot

Change Font Type in Matplotlib plots

In this tutorial, we’ll look at how to change the font types of texts in matplotlib plots.

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'})

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:

line chart with default font in matplotlib

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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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:

Line plot with 'fantasy' as its font family

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:

Scatter plot with "fantasy" as its font family.

You can see that the above scatter plot also has the text with the new default font family.

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:

Line chart with a different font for the axes title

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.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top