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:

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:

You can see that the plot title is now in italic text.
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.
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:

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:

You might also be interested in –
- How to bold text in a Matplotlib plot?
- Matplotlib – Remove the frame without altering the ticks and the tick labels
- Add Title to Each Subplot in Matplotlib
- Matplolib – Hide Axis in a Plot (Code with Examples)
- Matplotlib – Change the Number of Ticks in a Plot
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.