How to obtain the same font in Matplotlib output as in LaTex output?

To obtain the same font in Matplotlib output as in LaTeX output, you need to configure Matplotlib to use LaTeX for text rendering. This ensures mathematical expressions and labels appear with consistent LaTeX typography.

Method 1: Using LaTeX Text Rendering

Enable LaTeX rendering globally using rcParams ?

import numpy as np
import matplotlib.pyplot as plt

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

# Create figure
plt.figure(figsize=(8, 5))

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

# Plot with LaTeX formatting
plt.plot(x, y, 'b-', linewidth=2, label=r'$\sin(x)$')
plt.xlabel(r'$x$ (radians)', fontsize=12)
plt.ylabel(r'$y = \sin(x)$', fontsize=12)
plt.title(r'Graph of $\sin(x)$ using LaTeX fonts', fontsize=14)
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Method 2: Using mathtext with LaTeX-style Font

Configure mathtext to mimic LaTeX appearance without requiring LaTeX installation ?

import numpy as np
import matplotlib.pyplot as plt

# Configure mathtext to use LaTeX-style fonts
plt.rcParams['mathtext.fontset'] = 'cm'  # Computer Modern
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.size'] = 11

# Create data
x = np.linspace(-np.pi, np.pi, 200)
y1 = np.sin(x)
y2 = np.cos(x)

# Create plot
plt.figure(figsize=(8, 6))
plt.plot(x, y1, 'r-', label=r'$\sin(x)$', linewidth=2)
plt.plot(x, y2, 'b--', label=r'$\cos(x)$', linewidth=2)

plt.xlabel(r'$x$ (radians)')
plt.ylabel(r'$f(x)$')
plt.title(r'Trigonometric Functions: $\sin(x)$ and $\cos(x)$')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Method 3: Custom Font Properties

Use specific font properties to match LaTeX output ?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager

# Set font properties for LaTeX-like appearance
font_props = {
    'family': 'serif',
    'serif': ['Computer Modern', 'Times'],
    'size': 12
}

plt.rcParams.update(font_props)
plt.rcParams['mathtext.fontset'] = 'cm'

# Generate sample data
x = np.linspace(0, 4*np.pi, 300)
y = np.exp(-x/4) * np.sin(x)

# Create plot with LaTeX-style formatting
plt.figure(figsize=(9, 5))
plt.plot(x, y, 'g-', linewidth=2, label=r'$e^{-x/4}\sin(x)$')
plt.xlabel(r'$x$', fontsize=14)
plt.ylabel(r'$y$', fontsize=14)
plt.title(r'Damped Oscillation: $y = e^{-x/4}\sin(x)$', fontsize=16)
plt.legend(fontsize=12)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()

Comparison of Methods

Method LaTeX Required Font Quality Speed
text.usetex = True Yes Perfect match Slower
mathtext fontset No Very good Fast
Custom font properties No Good Fast

Key Configuration Parameters

  • text.usetex − Enable LaTeX text rendering
  • mathtext.fontset − Set math font ('cm' for Computer Modern)
  • font.family − Set font family ('serif' for LaTeX-like)
  • font.serif − Specify serif fonts to use

Conclusion

Use text.usetex = True for perfect LaTeX matching or mathtext.fontset = 'cm' for LaTeX-style fonts without requiring LaTeX installation. The Computer Modern font provides the closest match to standard LaTeX typography.

Updated on: 2026-03-25T20:11:41+05:30

361 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements