Introduction
Using LaTeX in plots helps make labels and text look professional and match your paper's style.
Jump into concepts and practice - no test required
Using LaTeX in plots helps make labels and text look professional and match your paper's style.
import matplotlib.pyplot as plt plt.title(r"$\alpha > \beta$ Example") plt.xlabel(r"Time ($s$)") plt.ylabel(r"Distance ($m$)") plt.plot([1, 2, 3], [1, 4, 9]) plt.show()
plt.title(r"Euler's formula: $e^{i\pi} + 1 = 0$")plt.xlabel(r"Voltage ($V$)") plt.ylabel(r"Current ($I$)")
plt.text(1, 5, r"$\frac{a}{b}$ is a fraction")
This code plots a simple quadratic curve and uses LaTeX to format the title and axis labels with math notation.
import matplotlib.pyplot as plt # Plot some data x = [0, 1, 2, 3, 4] y = [0, 1, 4, 9, 16] plt.plot(x, y) # Use LaTeX in labels plt.title(r"Quadratic Growth: $y = x^2$") plt.xlabel(r"Input $x$") plt.ylabel(r"Output $y$ (units)$") plt.grid(True) plt.show()
Make sure you have LaTeX installed on your system for full rendering support.
If LaTeX is not installed, matplotlib will use a fallback font but math may not look perfect.
Use raw strings (r"...") to avoid errors with backslashes in LaTeX commands.
LaTeX integration makes plot text look professional and consistent with papers.
Use raw strings and dollar signs to write math expressions in labels and titles.
This helps include symbols, formulas, and units clearly in your figures.
plt.rcParams['text.usetex'] = True do in matplotlib?plt.rcParams dictionary controls matplotlib's runtime configuration. Setting text.usetex to True tells matplotlib to use LaTeX to render all text elements.r) to avoid escape character issues.$...$ to render correctly.import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
plt.title(r'$\alpha + \beta = \gamma$')
plt.savefig('plot.pdf')
plt.rcParams['text.usetex'] = True enables LaTeX rendering for all text including titles.r'$\alpha + \beta = \gamma$' correctly formats Greek letters α, β, γ in math mode.import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
plt.xlabel('$x^2')
plt.show()'$x^2' has only one dollar sign, missing the closing $ to end math mode.E = mc^2 using LaTeX in matplotlib. Which code snippet correctly achieves this and saves the plot as a PDF with LaTeX-rendered text?plt.rcParams['text.usetex'] = True to use LaTeX for all text rendering.r'$E = mc^2$'.plt.savefig('energy.pdf') to save the plot with LaTeX-rendered title.