On using gnuplot

[back]


Introduction

I used {gnuplot} to create graphs for my master thesis, back in 2012. Then, I started to use Python’s matplotlib library, because by default the graphs looked more polished. I’m back to gnuplot, because:

General usage

The fastest way to plot from a file:

gnuplot '<file>' using 1:2 with lines title
  'data'

Gnuplot can be scripted. The standard extension is .gp. Plot from a script:

gnuplot <script>.gp

Calling from C

Define GNUPLOT as

#define GNUPLOT "gnuplot -persist"

Plot with gnuplot as

FILE *fp;
      fp = popen(GNUPLOT, "w");
      fprintf(fp, "plot '-' w p lt -1 pt 6 title 'e and z'\n");

      for (int i = 0; i < N * (ngp + 2); i ++)
      fprintf(fp, "%f %f\n", x[i], e[i]);
      fprintf(fp, "\033");

      fflush(fp);
      fclose(fp);

Specifics

Delimiters and comments

Specify the delimiter character:

set datafile separator
      "<char>"

Specify comment character:

set datafile commentschar
      "<char>"

Term

The terminal determines how the plot is displayed. I like the terminals dumb, qt, and png. Specify the terminal:

set term dumb # plots to shell
      set term qt   # graphical output
      set term png  # plot to image file

Font

The font can be specified through the term. For example

set term qt font "Times, 12"

Multiplot

Allows to plot several graphs in one figure, in a vertically or horizontally stacked way:

set multiplot layout 2,1 rowsfirst

      set title 'surface flow'
      plot '1.0e-06/surface_outlet_flux.dat' u 1:2 w l title 'i = 1.0e-06 m/s'

      set title 'subsurface flow'
      plot '1.0e-06/subsurface_outlet_flux.dat' u 1:2 w l title 'i = 1.0e-06 m/s'

      unset multiplot

Splot

Use this for plotting three-dimensional data:

splot "<file>" u 1:2:3 w l

Resources