How to Change Fonts in matplotlib?
Changing fonts in Matplotlib helps customize the appearance of plots, making them more readable and visually appealing. Fonts can be changed for titles, axis labels, legends and other text elements either individually or globally. Let’s explore how to do this efficiently.
Using fontname
This method lets you apply different fonts to specific parts of your plot, like axis labels or the title. It's a great choice when you want to mix and match fonts or highlight certain text.
Example 1: Change font of axis labels
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
t = np.linspace(0, 1, 1000, endpoint=True)
plt.plot(t, signal.square(2 * np.pi * 5 * t))
plt.xlabel("Time (Seconds)", fontname="Brush Script MT", fontsize=16)
plt.ylabel("Amplitude", fontname="Brush Script MT", fontsize=16)
plt.title("Square Wave", fontsize=18)
plt.show()
Output

Explanation: matplotlib.pyplot is used for plotting and scipy.signal generate a 5 Hz square wave over a time array from 0 to 1. The axis labels are styled with the "Brush Script MT" font and a font size of 16, while the title "Square Wave" uses a larger font size of 18 with the default font.
Example 2: Change Font of Title Only
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [0, 2, 4, 6, 8]
plt.scatter(x, y)
plt.title("Sample Scatter Plot", fontname="Franklin Gothic Medium", fontsize=18)
plt.show()
Output

Explanation: matplotlib.pyplot is used to plot a scatter graph of x and y, with the title "Sample Scatter Plot" styled in "Franklin Gothic Medium" font at size 18.
Example 3: Title and Axis Labels Together
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
plt.plot(x, y)
plt.title("Population Over Time", fontname="Franklin Gothic Medium", fontsize=18)
plt.xlabel("Years", fontname="Gabriola", fontsize=16)
plt.ylabel("Population (Million)", fontname="Gabriola", fontsize=16)
plt.show()
Output

Explanation: matplotlib.pyplot is used to plot a line graph of x and y, with the title "Population Over Time" in "Franklin Gothic Medium" at size 18. The x-axis and y-axis labels are styled in the "Gabriola" font with a font size of 16.
Using rcParams
If you want to apply one font style to your entire plot (titles, labels, ticks), use rcParams. This is efficient for maintaining consistency across multiple plots.
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.family'] = 'Comic Sans MS'
x = [1, 2, 3]
y = [2, 4, 1]
plt.plot(x, y)
plt.title("Global Font Example")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.show()
Output

Explanation: rcParams sets Comic Sans MS as the default font for all text elements in the plot. This means the title, x-axis label and y-axis label will automatically use this font without needing to specify it individually. A simple line plot is created using the x and y values.
Using matplotlib.rc
This is similar to rcParams, and you can use it to globally configure fonts. It’s especially useful when you want to apply settings in a modular way or within scripts/functions.
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc('font', family='Courier New')
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("Title with Courier New Font")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.show()
Output

Explanation: matplotlib’s rc sets Courier New as the default font for all text in the plot. The line plot uses the given x and y values, with the title and axis labels automatically styled in Courier New.
Using font_manager.FontProperties()
When you want to use a custom font file not installed on your system, this method allows you to load and apply it to any text element in your plot.
import matplotlib.pyplot as plt
from matplotlib import font_manager as fm
# Load a custom font file
path = '/path/to/your/font.ttf'
prop = fm.FontProperties(fname=path)
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("Custom Font", fontproperties=prop)
plt.xlabel("X-Axis", fontproperties=prop)
plt.ylabel("Y-Axis", fontproperties=prop)
plt.show()
Output

Explanation: This code loads a custom font from a specified file path using font_manager.FontProperties. The font property prop is then applied individually to the plot’s title, x-axis label and y-axis label.