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.title("A lovely plot")

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

| improve this answer | |

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.