Introduction
Matplotlib is a data visualization library that allows you to create a variety of visualizations in Python. One important aspect of creating visualizations is selecting the appropriate font for your text. In this tutorial, we will learn how to use PGF fonts in Matplotlib.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import necessary libraries
We will start by importing the necessary libraries for this tutorial. We will be using matplotlib.pyplot to create our visualization.
import matplotlib.pyplot as plt
Set the font family
We will set the font family to "serif" using the font.family parameter. Additionally, we will set the font.serif parameter to an empty list to use the default LaTeX serif font.
plt.rcParams.update({
"font.family": "serif",
"font.serif": [],
})
Set the cursive fonts
We will set the cursive fonts using the font.cursive parameter. In this example, we will use "Comic Neue" and "Comic Sans MS".
plt.rcParams.update({
"font.cursive": ["Comic Neue", "Comic Sans MS"],
})
Create the plot
Next, we will create a simple line plot using the ax.plot() function.
fig, ax = plt.subplots(figsize=(4.5, 2.5))
ax.plot(range(5))
Add text to the plot
We will add text to the plot using the ax.text() function. We will add text to four different locations on the plot, each with a different font family: serif, monospace, sans-serif, and cursive.
ax.text(0.5, 3., "serif")
ax.text(0.5, 2., "monospace", family="monospace")
ax.text(2.5, 2., "sans-serif", family="DejaVu Sans")
ax.text(2.5, 1., "comic", family="cursive")
Set the x-label
We will set the x-label using the ax.set_xlabel() function. We will use the Greek letter mu as an example.
ax.set_xlabel("µ is not $\\mu$")
Save the plot
Finally, we will save the plot as a PDF and PNG file using the fig.savefig() function.
fig.tight_layout(pad=.5)
fig.savefig("pgf_fonts.pdf")
fig.savefig("pgf_fonts.png")
Summary
In this tutorial, we learned how to use PGF fonts in Matplotlib. We set the font family, cursive fonts, created a plot, added text, set the x-label, and saved the plot. These techniques can be used to customize the appearance of your visualizations in Matplotlib.