The three ways to change fonts in Matplotlib
Matplotlib lets you change fonts in three ways: by setting a single font for your entire chart, by changing the font of specific text elements like titles or axis labels, or by using a font file stored on your computer. The method you choose depends on whether you want one consistent look across all your charts or need different fonts in different places.
The simplest approach is to set a default font before you create your chart. This changes every text element at once. The second method targets individual pieces — you can make your title bold while keeping axis labels in a different style. The third method loads custom fonts from your system, useful if your organization requires a specific typeface.
Key Takeaways
- Use plt.rcParams['font.sans-serif'] to set a default font for all text in your chart before plotting.
- Pass the fontname parameter directly to individual text functions like plt.title(), plt.xlabel(), and plt.ylabel() to change specific elements.
- Matplotlib searches your system fonts first, so any font installed on your computer is available by name without extra setup.
- Use matplotlib.font_manager.findfont() to check whether Matplotlib can locate a font before you use it in your code.
- The fontsize parameter works alongside fontname to control both the typeface and the size in a single command.
Setting a default font for your entire chart
The fastest way to change fonts across your whole chart is to modify plt.rcParams before you plot anything. This tells Matplotlib to use your chosen font for every text element — title, labels, tick marks, and legend.
Add these lines at the top of your script, right after your import statements:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['Arial'] plt.rcParams['font.size'] = 12
Replace 'Arial' with any font name installed on your system. Common options include 'Times New Roman', 'Courier New', 'Verdana', and 'Georgia'. The font.size line sets the default size in points. Every chart you create after this point will use Arial at 12 points until you change the setting again or close your Python session.
If you want to use multiple fonts and let Matplotlib pick the first one it finds, pass a list instead of a single name:
plt.rcParams['font.sans-serif'] = ['Arial', 'Helvetica', 'DejaVu Sans']
Matplotlib will use Arial if it exists on your system, fall back to Helvetica if Arial is missing, and use DejaVu Sans if neither is found. This approach is useful when sharing code with others who may not have the same fonts installed.
Changing fonts for specific text elements
When you need different fonts in different places, pass the fontname parameter directly to the text function. This overrides your default setting for just that element.
Here is how to change the title, axis labels, and tick labels separately:
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [1, 4, 9]) ax.set_title('My Chart', fontname='Times New Roman', fontsize=16) ax.set_xlabel('X Axis', fontname='Arial', fontsize=12) ax.set_ylabel('Y Axis', fontname='Courier New', fontsize=12) plt.show()
The fontname parameter works with plt.title(), plt.xlabel(), plt.ylabel(), and ax.text() for adding custom text anywhere on your chart. You can also change the font of tick labels — the numbers along your axes — using ax.tick_params() or by looping through the tick labels and setting their font individually.
To change tick label fonts, use this approach:
for label in ax.get_xticklabels(): label.set_fontname('Arial') for label in ax.get_yticklabels(): label.set_fontname('Arial')
Using fonts installed on your computer
Matplotlib reads fonts from your system's font directory automatically. On Windows, this is usually C:\Windows\Fonts. On Mac, it is /Library/Fonts. On Linux, fonts are typically in /usr/share/fonts. You do not need to download or install anything extra — just use the font name as it appears in your system.
If you are unsure whether a font is installed or what its exact name is, use matplotlib.font_manager to check:
from matplotlib import font_manager fonts = [f.name for f in font_manager.fontManager.ttfonts] print(fonts)
This prints a list of every font Matplotlib can find on your system. Search this list for the font you want to use. If your font does not appear, it is not installed, and you will need to install it through your operating system before Matplotlib can use it.
You can also check whether a specific font exists before using it:
from matplotlib.font_manager import findfont, FontProperties font_path = findfont(FontProperties(family='Arial')) print(font_path)
If the font exists, this prints the path to the font file. If it does not exist, Matplotlib returns the path to a fallback font instead.
Combining font changes with other text properties
The fontname and fontsize parameters work alongside other text styling options. You can make text bold, italic, or both in the same command:
ax.set_title('My Chart', fontname='Arial', fontsize=14, fontweight='bold', fontstyle='italic')
Common values for fontweight are 'normal', 'bold', and 'heavy'. For fontstyle, use 'normal', 'italic', or 'oblique'. You can also control text color with the color parameter and alignment with ha (horizontal alignment) and va (vertical alignment):
ax.set_xlabel('X Axis', fontname='Times New Roman', fontsize=12, color='darkblue', fontweight='bold')
These parameters work together, so you can style your text as precisely as you need in a single function call.
Saving charts with custom fonts
When you save a chart to a file using plt.savefig(), Matplotlib embeds your font choices into the output. However, the behavior depends on your file format. PNG and JPEG files render fonts as pixels, so the font choice is baked into the image and will look the same on any computer. PDF and SVG files can embed font information, but some fonts may not embed correctly depending on licensing restrictions.
If you are saving to PDF and need to ensure fonts display correctly on other systems, test your output on a different computer before sharing. If a font does not embed, Matplotlib will substitute a fallback font when the PDF is opened elsewhere.
For the most reliable results across different systems, stick to common fonts like Arial, Times New Roman, and Courier New. These are installed on nearly all computers and will display consistently whether your chart is viewed as an image or a PDF.
Frequently Asked Questions
What happens if I use a font name that does not exist on my computer?
Matplotlib will not raise an error. Instead, it silently substitutes a default font, usually DejaVu Sans. Your chart will still display, but it will not use the font you specified. To avoid this, check whether your font exists using findfont() before you use it, or stick to widely available fonts like Arial and Times New Roman.
Can I use a font file from my Downloads folder instead of a system font?
Yes. Use matplotlib.font_manager.fontManager.addfont() to load a font file directly from any location on your computer. Pass the full path to the font file, and Matplotlib will add it to its available fonts for the current session. After that, you can use the font by name in your charts.
Why does my chart look different when I open it on a different computer?
The most common reason is that the font you used is not installed on the other computer. Matplotlib substitutes a fallback font, which changes the appearance. Use common fonts or embed your fonts in your output file format to avoid this problem.
How do I change the font of the legend in my chart?
Pass the prop parameter to ax.legend() with a FontProperties object. For example: ax.legend(prop=FontProperties(family='Arial', size=10)). This controls both the font name and size for all text in the legend.
Can I use different fonts for different lines in the same chart?
Yes, but it requires more work. You cannot set a font for a data line itself, but you can add text labels near each line using ax.text() with different fonts for each label. For axis labels and titles, you can already use different fonts by calling the appropriate function with its own fontname parameter.