make certain text in matplotlib plot italic

How to make text italic in a Matplotlib plot?

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

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

# make the title italic
plt.title("My plot title", fontstyle='italic')

Let’s now look at some examples of using italicized texts 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:

line chart with title

Example 1 – Make the plot title italic

Let’s now plot the above line chart again but this time with the plot title italicized. For this, we pass an additional keyword argument, fontstyle to the matplotlib.pyplot.title() function.

# 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", fontstyle='italic')

Output:

resulting line chart with title in italics

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

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

Other valid values for the fontstyle parameter are – 'normal' which is the default value and 'oblique' which gives the same result as 'italic'.

Example 2 – Make the axes labels italic

We can similarly make the axes labels italic by using the fontstyle keyword argument when setting the respective axes labels.

Let’s use italic font in both the x-axis and the y-axis labels.

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

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

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

Output:

line chart with the axes labels in italics

Now, the axes labels are italicized.

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

Let’s make the title of the original line plot bold and also italics.

# 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', fontstyle='italic')

Output:

line chart title both bold and italicized

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