Skip to Content

How to bold text in a Matplotlib plot?

In this tutorial, we will look at how to bold the text appearing in different parts of a matplotlib plot with the help of some examples.

To bold text in a matplotlib plot, you can use the keyword argument fontweight and pass 'bold' as its value when adding different texts to a plot. For example, to bold the title of a matplotlib plot, use the fontweight argument when assigning the title using matplotlib.pyplot.title() function.

# bold the title 
plt.title("My plot title", fontweight='bold')

Let’s now look at some examples of using bolding text in a matplotlib plot.

First, let’s create a line plot with some text as the plot title and the axes titles with the default font settings.

import matplotlib.pyplot as plt

# x values - years
x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
# y values - curde oil price per barrel in USD
y = [94.88, 94.05, 97.98, 93.17, 48.66, 43.29, 50.80, 65.23, 56.99, 39.68]

# plot x and y on a line plot
plt.plot(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

# add plot title
plt.title("Crude oil prices in 2010s")

Output:

the resulting line chart

Example 1 – Bold the plot title in matplotlib

Let’s now plot the above line chart again but this time with the plot title bolded.

# plot x and y on a line plot
plt.plot(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

# add plot title
plt.title("Crude oil prices in 2010s", fontweight='bold')

Output:

line chart with bold title text

You can see that the plot title is now in bold text.

Example 2 – Bold the axes labels in matplotlib

We can similarly make the axes labels bold by using the fontweight keyword argument when setting the respective axes labels.

Let’s make both the x-axis and the y-axis labels bold.

# plot x and y on a line plot
plt.plot(x, y)

# add axes labels
plt.xlabel('Year', fontweight='bold')
plt.ylabel('Crude oil price USD/barrel', fontweight='bold')

# add plot title
plt.title("Crude oil prices in 2010s")

Output:

axes labels bold in the line chart

Now, the axes titles are bold.

You can similarly format the text with other keyword arguments such as fontsize, horizontalalignment, etc.

Let’s make the title of the original line plot bold and also increase its font size.

# plot x and y on a line plot
plt.plot(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

# add plot title
plt.title("Crude oil prices in 2010s", fontweight='bold', fontsize=18)

Output:

line chart title is bold and has a larger font size

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.