Thursday 4 January 2024

Gnuplot 6.0.0

 I noticed that Gnuplot 6.0.0 is out.  Details of new features can be seen in the release notes.  I'm excited about the possibility of sector plots, in particular, it can make "polar equivalent to sparse-matrix heatmaps."  This is a kind of plot I do actually need to make for my research, and have outsourced the work to knowledgeable postdocs, or made somewhat ugly ones with matplotlib.  I'm keen to see what I can do with Gnuplot (while acknowledging that it is me, rather than matplotlib per se that stop me from making the plots I want to with mpl)

Monday 22 June 2020

Separate y-axis on the right

If you want to have two independent y-axis – one on the left, and one on the right – you can unset the mirroring of the "main" left-hand y-axis marks with set ytics nomirror, and use axis x1y2 in the plot command to use the right-hand y-axis.  See the following example for a basic unembellished example

gnuplot> set y2tics
gnuplot> set ytics nomirror
gnuplot> set ylabel "Energy [MeV]"
gnuplot> set y2label "{/Symbol b}_2"
gnuplot> set termopt enh
gnuplot> set xlabel "index"
gnuplot> plot 'ensurf.all' u 0:13 w l t "Binding Energy", '' u 0:4 axis x1y2 w l t "deformation ({/Symbol b}_2)"


and the result is shown in the screengrab below


Tuesday 3 July 2018

Dash it all: Custom line styles

Lines in gnuplot can come in different styles;  solid lines, dashed lines, or dotted lines, for example.  Probably the easiest way to set these is to use the dashtype pattern specifier, in gnuplot 5 and above.  The gnuplot help guide says:

       dashtype "pattern"  # string containing a combination of the characters
                           # dot (.) hyphen (-) underscore(_) and space.

and some examples are shown below:


set term pdfcairo font "Chalkduster,15"
set out 'dashes.pdf'
set key samplen 12 opaque box
PI = acos(-1.)
set xrange [0:2*PI]
plot sin(x) dashtype '-' lw 3 t 'Sin(x)', \
  sin(2*x) dashtype '.' lw 3 t 'Sin(2x)', \
  sin(3*x) dashtype '. -   _' lw 3 t 'Sin(3x)', \
0 not

Here we set up a pdf output, with a somewhat silly choice of font -- and one that I checked existed on my system.  I set up the key to include a very long length line in the legend, because I am making up an example with a complicated dash type.

The plot line plots the functions sin(nx), n=1,2,3 and assigns each a different dash type:  '-', '.' and '. -  _'for dashes, dots, and a more complicated pattern.  The results are shown below:


Friday 19 February 2016

A pm3d surface plot

Here is a quick example of visualising a function of two variables using the pm3d "palette-mapped" 3d plot.  It comes from a module I teach on computational techniques and is the end result of a solution of the Laplace equation for the electrical potential, in a square region in which the potential along the upper boundary of the square is held at +100V and is 0V along the other boundaries.  The data, in the file pot.dat, is here.
# set up the terminal to be the interactive wxt display, 
# but with equal width and height
set term wxt size 400,400

# we will be plotting a function of two variables, but we 
# don't want the default surface plot
unset surface

# view from above
set view 0,90

# set the coloured "pm3d" plot (rather than the surface, 
# which we unset above)
set pm3d

# viewing from above, we don't want to have the z-axis
# tick marks shown all bunched up
unset ztics

# now plot, with a title
splot 'pot.dat' title "Potential"
The result, loaded into gnuplot with a load './pot.gpl' command is shown below:


It's not perfect.  With a bit of tweaking with placement, the title could be centrally above the square of the plot and the whitespace more even in the frame, but it visualises the potential quite well.

Tuesday 12 January 2016

Quicker and dirtier one-line printing

In a previous post, I wrote about how to use a for loop and the system command to plot several data sets from different files in one go.  Taking that idea a bit further, one can make use of the dumb terminal type and a Unix pipe (assuming one is using a variety of Unix, such as OS X or Linux) to have a completely self-contained one-liner from a Unix shell.  This can be particularly useful for a quick way to check results running on a distant server that you want to connect to remotely via ssh and don't have the bandwidth to comfortably bring up graphical windows via X.

An example, building on the previous one, along with the result, is shown below:

% echo "set term dumb ; plot for [file in system('ls -1 */quadrupoles.res')] file u 1:4 w l t file" | gnuplot



  1200 ++---------+-----------+----------+----------+-----------+---------++
       *          +           +          +    66mev/quadrupoles.res ****** +
       *                                      69mev/quadrupoles.res ###### |
  1000 +*                                     70mev/quadrupoles.res $$$$$$++
       |*                                     71mev/quadrupoles.res %%%%%% |
       |#*                                                                 |
   800 ++*                                                                ++
       | *                                                                 |
       |  *                                                                |
   600 ++ #*                                                              ++
       |   *                              %                                |
       |   %*                         %%%%$$$$$$$$$$$$$$$$$$$$             |
       |    *                   %$############               $$$$$$$$$     |
   400 ++   #*               %##*******       ####                    $$$$++
       |     **          %******       ****      ##**                     $$
       |      **       ****                ***   **                        |
   200 ++      **    ***                      ***                         ++
       |        ** %**                                                     |
       +         ***          +          +          +           +          +
     0 ++---------+-----------+----------+----------+-----------+---------++
       0         100         200        300        400         500        600


Now, along with the plot for [file in ... construct, we have an extra gnuplot command set term dumb to make the output be character-based in the terminal window.  The Unix echo command prints the whole set of gnuplot instructions, and those are then piped to the gnuplot program using the Unix pipe symbol (|).

The dumb terminal by default uses a 'display' which is 79x24 characters, which is suitable for most default situations.  If you are running from a Terminal window in a windowing environment (the most common situation at the time of writing) then you can always drag your terminal to be much bigger and then request a more detailed plot.  The result is shown as a graphic below (unlike the above literal text result) so that it can be squashed down to fit in the blog post.  When used in anger, the font is just the same as the example above.  The difference in the command leading up to this figure is in writing set term dumb 300 75 to give 300 characters in the x direction and 75 in the y direction.

gnuplot textual plot


Tuesday 21 July 2015

A quick one-liner for plotting multiple files

I have a series of directories, each with a file of the same name, containing numerical data to do with calculations of nuclear fusion.  Each data set differs by the starting condition, and I'd like to be able to quickly show all the different calculations together, and see a visual update of them all, while my code is generating the files.

A quick one-liner, which does this is


plot for [file in system("ls -1 */qmom")] file u 0:1 w l t file


In this case, the file in each directory is called qmom.  The for loop syntax for [a in list] allows an arbitrary list of text strings to be looped over and assigned to a variable.  This allows for a really quick way to produce a plot comparing data from several files.

Of course, extra commands to prettify the plot to one's taste would probably be needed for a production plot.  The attached plot has had a bit of such stuff done (as well as adding labels etc.)


Tuesday 23 July 2013

Beautiful splotting

Recently, I've been doing rather a lot of R-matrix analysis. In short this means I'm trying to understand the probability of nuclear reactions occurring as a function of both energy and scattering angle. The gnuplot splot command is great for viewing the calculations and data as a function of two variables, and makes fitting the data much easier.

However, with many data points, depth perception can get a little tricky. For example, a typical plot might look something like this:


In this case, the agreement between the curve and the points isn't fantastic, but it simply isn't clear which point should be compared to which line.  You can't easily distinguish both the x and y coordinates of each point.  Even interactively rotating the plot doesn't help much when the point density is this high.

The tip here is to colour the points and calculations to help depth perception.  Adding the palette option to the splot command will colour by a fourth variable that is appended to the column specification.  Here's the sample code:

set terminal png enhanced size 800,600 font "sans, 16"

set xlabel "Energy (MeV)"
set ylabel "Angle (degrees)" offset 0,-1,0
set zlabel "Cross section (mb)" rotate by 90

set palette model RGB defined ( 0.0 'black', 0.3 'purple', 0.5 "blue", 0.7 "green", 0.9 "orange", 1.0 "red" )

set output "splotting.png"  
splot "data.dat" u 1:3:6:3 w p ps 1 palette notitle,\
      "data.dat" u 1:3:4:3 w l lw 2 palette notitle

In this case, all the data is in data.dat.  Columns 1 and 3 contain the energy and angle respectively.  Column 4 and 6 contain the data points and the calculations.  The fourth specifier (3) tells gnuplot to colour things by the angle - in this case, the y-axis variable.  The resulting plot looks like this:


It is now clear which lines should be compared to which points.  The palette option certainly makes for prettier splots and can be really useful for improving their readability. The range of colours can be tuned in each case using the set palette model command to match the spread of your data to make things as clear as possible.