In order to include plots created with matplotlib in TeX documents, they should be saved as pdf
or eps
files. In this way, any text in the plot (including TeX formulae) is rendered as text in the final document.
import matplotlib.pyplot as plt
plt.rc(usetex=True)
x = range(0, 10)
y = [t**2 for t in x]
z = [t**2+1 for t in x]
plt.plot(x, y, label=r'$\beta=\alpha^2$')
plt.plot(x, z, label=r'$\beta=\alpha^2+1$')
plt.xlabel(r'$\alpha$')
plt.ylabel(r'$\beta$')
plt.legend(loc=0)
plt.savefig('my_pdf_plot.pdf') # Saving plot to pdf file
plt.savefig('my_eps_plot.eps') # Saving plot to eps file
Plots in matplotlib can be exported to TeX code using the pgf
macro package to display graphics.
import matplotlib.pyplot as plt
plt.rc(usetex=True)
x = range(0, 10)
y = [t**2 for t in x]
z = [t**2+1 for t in x]
plt.plot(x, y, label=r'$\beta=\alpha^2$')
plt.plot(x, z, label=r'$\beta=\alpha^2+1$')
plt.xlabel(r'$\alpha$')
plt.ylabel(r'$\beta$')
plt.legend(loc=0)
plt.savefig('my_pgf_plot.pgf')
Use the rc
command to change the TeX engine used
plt.rc('pgf', texsystem='pdflatex') # or luatex, xelatex...
To include the .pgf
figure, write in your LaTeX document
\usepackage{pgf}
\input{my_pgf_plot.pgf}