Skip to content

Latex, Python, and CairoPlot

CairoPlot is a Python module that uses the Cairo graphics package to produce great-looking charts easily. The results look really nice and are much simpler to create than many other packages, but up until now it has been suboptimal for use with LaTeX documents. I’ve been talking with the package maintainer and filing bugs about these inadequacies, and my concerns were quickly addressed. See the CairoPlot Launchpad page to view the bugs filed against it.

I also recently found a blog post about embedding python in LaTeX files. Using this with CairoPlot, it is easy to put the chart-producing code directly in my .tex files and compile. This reduces the extra step of making separate python scripts to produce these charts. Here is some sample code from a paper I’m currently writing:

\begin{python}
import cairoplot
cairoplot.vertical_bar_plot(
'dat/initial-grammar-stats.ps',
[ [0.87, 0.82], [0.83, 0.50], [0.70, 0.11] ],
340, 280, background = None, border = 10, grid = True,
x_labels = ['Parses', 'Generates', 'Generates Original'],
y_labels = ['%d%%' % i for i in range(0,110,10)],
y_bounds = (0,1) )
print(r'\includegraphics{dat/initial-grammar-stats.ps}')
\end{python}

I do have some gripes about python.sty, though. I have to remember to use the latex command with the “–shell-escape” option. Also, it produces files for the python code, stderr and stdout output, and I don’t particularly care for the directory getting cluttered up with temporary files. Because of these annoyances, I might forgo the python.sty method and just keep a python script that generates all the charts.

One Comment

  1. Hey man, thanks for the mention!
    I’ll be addressing the new bugs soon.
    Regards

    Posted on 19-Mar-09 at 3:41 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*