Generating plots with pgfplots

Plotting results is often needed in Computer Science.
Say, you have a program that generates data which you want to plot.

A normal approach to this would be:

  1. Write your results to a csv-file
  2. Importing that file into a spreadsheet application like Excel
  3. Clicking around to generate a plot
  4. Saving the plot as an image
  5. Putting the image into a LaTeX report

In case you change something in your program, this entire process would have to be repeated in order to get your fresh results plotted and into your report.

How cumbersome. This is a stupid problem we can solve with programming.
If our program already generates data and writes it to disk in text format, we can just add functions that write that data as LaTeX!

Introducing PGFplots

PGFPlots is a nice package for LaTeX that does exactly what it says. It creates plots. There’s a gallery full of examples right here:
There are endless possibilities and this post will focus on generating plots from data generated by a program, not on the capabilities of pgfplots. Check out the main site for the package if you want to know how pgfplots works and what it can do.

A pgfplot can look like this:

Rendered by

It is a made up example, plotting two data sets apples and oranges.

The code that generates this plot is:

Writing a program to do the hard work

We can break the structure of this into its discrete parts, identify the static parts and the changing parts.

All plots start and end with these two lines:

We then have a “preamble” with 8 changing parts: the height and width of the plot, the four coordinates defining the values in the coordinate system and the labels of the x and y axis:

Last but not least we have the actual plot. For each thing we want to plot, we have a static part that always has to be there and a changing part, our data points and the label we want to go in the legend of the plot.

In my example, the labels are apples and oranges, each representing my made up data sets.

Breaking this down further, we have

Writing functions that generates LaTeX for our plots is easy. We just need to inject our data into the static parts of a plot!

I will use F# for this but any language that can generate a string can be used.

First a long codedump that generates the example plot, then explanations of each part of the code:

Not too bad. Now for the explanations.

We start by converting our data to something Tikz can understand. (Tikz is what pgfplots uses to draw the plots).

We have our data as tuples of floats. F# lets us convert the tuple into a string, using the format string %A. We want to separate the elements by newline. We accomplish this with a fold over our list of data, generating the desired string for each element of our data.

We now want to wrap the plot coordinates in the
\addplot coordinates{...} part and add the label for our legend entry
in the
\addlegendentry{...} part.

Next we want to wrap that inside the
\begin{axis}...\end{axis} part. We need to generate the axis preamble before we can do that!

This function seems a bit messy but actually there is not much going on, just a lot of format strings.

The function takes these parameters as input:

  • height : int
  • width : int
  • xmin : int
  • xmax : int
  • ymin : int
  • ymax : int
  • xlabel : string
  • ylabel : string

Now that we can generate our axis preamble, we want to inject our plots in between
\begin{axis}[...] ... \end{axis}.

We are almost done! We just need to wrap our generated string in between \begin{tikzpicture}...\end{tikzpicture}.

The following piece of code does exactly that.

Putting it all together, we end up with the many lines of code I showed earlier.
The generated LaTeX-string can be saved to disk and used in any LaTeX report.

Getting the plot into a LaTeX report

LaTeX has a command for including LaTeX files. This is useful if you are writing e.g. a book and want to split your chapters into separate files.
It is also useful to us, as we can save the generated plot as a .tex file and just include that.
If we have a LaTeX file called report.tex and in the same directory have a file called ourGeneratedPlot.tex, we can include the plot in our report in the following way:

No more clicking around in excel!

Happy generating plots.

Leave a Reply