# Set fonts in matplotlib graphs equal to tex document

I am writing my PhD in LaTeX and I am generating all my graphical content in Python with the matplotlib package. For my TeX document, I am using a math-supported font -- e.g. Kurier Light Condensed -- and I would like to use the same font in my plots.

What is the easiest way to use the same font in my `matplotlib` figures and my corpus text in LaTeX?

• e.g. can I save my figures such that it takes the TeX font when importing in TeX?
• e.g. can I import the font in `matplotlib` and use it there?
-

There is `matplotlib2tikz`, which creates a TikZ/pgfplots file that can be `\input` in your document. I don't know how well it works, having never used `matplotlib`, but I have used `matlab2tikz` from the same author, and that works well. Also, I do not know if `matplotlib2tikz` supports all the different kinds of plots that `matplotlib` can create – in the Matlab case there are some limitations I believe.

Edit: For reference, here is a brief explanation of installation and use.

Having downloaded the files from github, navigate to the folder in a command line, and run `python setup.py install` to install `matplotlib2tikz`.

In your Python script add `from matplotlib2tikz import matplotlib2tikz`, and add `matplotlib2tikz('filename.tex')` after your plot, e.g.

``````from numpy import arange
from matplotlib2tikz import matplotlib2tikz
import matplotlib.pyplot as plt

x = arange(0,10,0.5)

plt.plot(x,x**2)
plt.title("A lovely plot")
plt.ylabel("\$y=x^2\$")
matplotlib2tikz('test.tex')
``````

You can also specify the width and height of the plot in the Python script, as mentioned in the readme of `matplotlib2tikz`.

-

Matplotlib now includes a PGF backend as of the 1.2 release. See this GitHub issue.

-