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 [UPDATE: post appears to have gone offline. Here is a backup copy of python.sty, thanks to Steve Checkley]. 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:

1
2
3
4
5
6
7
8
9
10
11
\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.

This entry was posted in Software, Uncategorized and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted 2009.03.19 at 03:41 | Permalink

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

One Trackback

  1. [...] embedding Cairoplot in LaTeX there is some example code at Michael Wayne Goodman's blog (goodmami.org) Some example code for a dot plot from the Cairoplot web page is below: dot_line_plot (name, data, [...]

Post a Comment

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

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>