How do I change the axis tick font in a Matplotlib plot when rendering using LaTeX?

To change the axis tick font in Matplotlib when rendering using LaTeX, you can use LaTeX commands within the tick labels. This allows you to apply various font styles like bold, italic, or different font families.

Basic LaTeX Font Styling

Here's how to create a plot with custom LaTeX−styled tick fonts ?

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.array([1, 2, 3, 4])
y = np.exp(x)

ax1 = plt.subplot()
ax1.set_xticks(x)
ax1.set_yticks(y)
ax1.plot(x, y, c="red")

# Set bold font using LaTeX
ax1.set_xticklabels(["$\bf{one}$", "$\bf{two}$", "$\bf{three}$", "$\bf{four}$"], rotation=45)
ax1.set_yticklabels(["$\bf{" + "{:.2f}".format(val) + "}$" for val in y], rotation=45)

plt.tight_layout()
plt.show()

Different LaTeX Font Styles

You can use various LaTeX commands for different font effects ?

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(2, 2, figsize=(10, 8))
x = np.array([1, 2, 3, 4])
y = np.array([2, 4, 6, 8])

# Bold font
axes[0, 0].plot(x, y)
axes[0, 0].set_xticklabels(["$\bf{A}$", "$\bf{B}$", "$\bf{C}$", "$\bf{D}$"])
axes[0, 0].set_title("Bold Font")

# Italic font
axes[0, 1].plot(x, y)
axes[0, 1].set_xticklabels(["$\it{A}$", "$\it{B}$", "$\it{C}$", "$\it{D}$"])
axes[0, 1].set_title("Italic Font")

# Math font
axes[1, 0].plot(x, y)
axes[1, 0].set_xticklabels(["$\mathcal{A}$", "$\mathcal{B}$", "$\mathcal{C}$", "$\mathcal{D}$"])
axes[1, 0].set_title("Calligraphic Font")

# Sans-serif font
axes[1, 1].plot(x, y)
axes[1, 1].set_xticklabels(["$\mathsf{A}$", "$\mathsf{B}$", "$\mathsf{C}$", "$\mathsf{D}$"])
axes[1, 1].set_title("Sans-serif Font")

plt.tight_layout()
plt.show()

Common LaTeX Font Commands

LaTeX Command Description Example
$\bf{text}$ Bold font "$\bf{Label}$"
$\it{text}$ Italic font "$\it{Label}$"
$\mathcal{text}$ Calligraphic font "$\mathcal{L}$"
$\mathsf{text}$ Sans-serif font "$\mathsf{Label}$"

Enabling LaTeX Rendering

For full LaTeX support, you can enable LaTeX text rendering globally ?

import numpy as np
import matplotlib.pyplot as plt

# Enable LaTeX rendering
plt.rcParams['text.usetex'] = True
plt.rcParams['font.family'] = 'serif'

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

plt.figure(figsize=(8, 5))
plt.plot(x, y)
plt.xticks([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi], 
           [r'$0$', r'$\frac{\pi}{2}$', r'$\pi$', r'$\frac{3\pi}{2}$', r'$2\pi$'])
plt.xlabel(r'\textbf{Angle (radians)}', fontsize=12)
plt.ylabel(r'\textit{Amplitude}', fontsize=12)
plt.title(r'$\mathbf{Sine\ Wave}$', fontsize=14)

plt.show()

Conclusion

Use LaTeX commands like $\bf{text}$ for bold or $\it{text}$ for italic tick labels. Enable text.usetex = True for full LaTeX rendering support with more advanced typography options.

Updated on: 2026-03-25T20:13:19+05:30

607 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements