This post lays out an end-to-end working version of a script that works on Fedora 22 (at a minimum) for producing matplotlib output that can be directly incorporated into LaTeX documents, and has the right font sizes, etc. without going through a raster-graphics layer.

The intermediate .pgf graphics files are also acceptable to arXiv, and can be loaded up alongside your original .tex source file.

### Preparation

On my system that was already set up to produce regular LaTeX documents, the following additional packages

### Basic Imports and Arguments

For stylistic consistency, I chose to have one central script that produces all the graphs required. Simply call it without arguments to produce all the charts for embedding.

Calling it with --latex=0 will instead display a popup window with the output for quicker debugging of the code. And there is an option --figure=XXX that can be used to focus on one figure type only. Doing this speeds up the interation speed tremendously.

### LaTeX defaults for matplotlib

This code was essentially taken from :

• http://bkanuka.com/articles/native-latex-plots/

• http://nipunbatra.github.io/2014/08/latexify/

and modified to bring it up-to-date.

### Code for a matplotlib figure

This is produced (if the command line arguments suggest it should be) by parsing a log file output by one of my training algorithms, so there’s some sample log parsing in there.

( note that the b: 0 match is done so specifically because I wanted to capture each epoch’s batch=zero, and was in a bit of a hurry… )

The data is plotted against two independent y-axes, and is finally saved (or displayed, depending on --latex=X).

By making use of latexify(), format_axes() and savefig() the whole screen vs page debate is deferred until you can get the graph looking right interactively.

Note that mathmatical notation can be included in any of the text - just introduce within the \$ context - and make sure that \ is doubled up - Python will ‘use up’ the first level of escaping.

### Code for the LaTeX document itself

Near the top of your .tex file you’ll need to include the pgf package :

And the figure itself can be included (and referred to) as follows :