Simple Line Plots: Using PGFPLOTS

This post belongs to a series of tutorials about how to draw simple line plots for academic papers. Information about the data used is available in the first post of the series, and the source code is on GitHub. Here I will focus on using PGFPLOTS, which is a package for TeX. The other two implementations are done using matplotlib and using ggplot2.

The preamble used in this implementation imports the PGFPLOTS package and has a couple of options to enable the plot to be exported.


The data to be plotted is divided in three files, but this times there is no need to combine them before the actual drawing. We can just pass the file name and the points in the file will be plotted by PGFPLOTS:

      \addplot [mark=*] table [x=size, y=time, col sep = comma]{gpu.csv};
      \addplot [mark=triangle*] table [x=size, y=time, col sep = comma]{cpuParallel.csv};
      \addplot [mark=square*] table [x=size, y=time, col sep = comma]{cpuSerial.csv};

It is needed, though, to place the plots in a axis tag (in this case, semilogyaxis), which is inside a tikzpicture tag, which is inside a figure tag. And everything belongs to inside a document tag. The result of this first step is as follows:


Next, we make the fonts and the markers bigger, as well as some options for the legend that will be added later:

    every plot/.append style={mark size=4pt, ultra thick, color=black},
    every tick label/.append style={font=\Large},
    every axis label/.append style={font=\Large},
    every axis legend/.append style={legend pos=north west, draw=none, fill=none, very thick, font=\Large, nodes=right},

These changes are inserted in the tikzset tag, which is placed before the actual plot. Although we need to go back in the code a bit, that’s the easiest way I found to explain the steps. The result is:


Finally, we make some modifications like the size of the figure, the limits of the axes, removing the minor axis ticks, inserting a horizontal grid, specifying the axes’ labels and the legend entries. To do that, we actually have to modify the previous code a bit and pass these options to the axis tag (semilogyaxis in this case):

  major grid style={dotted},
  tick pos = left,
  xlabel = {$|R|$},
  ylabel = {Elapsed time (s)},
  xticklabels={$2^{10}$, $2^{11}$, $2^{12}$, $2^{13}$, $2^{14}$, $2^{15}$, $2^{16}$, $2^{17}$, $2^{18}$, $2^{19}$},
  legend entries={GPU, CPU (Parallel), (CPU Serial)},
  reverse legend
% ...

This gives us the final version of the plot:

pgffinalAs mentioned, the full source file is available at GitHub. The comment section is open for discussion and suggestions about the design choices for the plot or about the way they were implemented in this tutorial.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s