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.

Tuesday, 30 April 2013

Bar Charts

I wanted to make a bar chart for a recent post on my real blog (on the difference between different citation databases for my recent papers).

I needed to look up enough of the details of how to do it in gnuplot, that I thought I'd document it here for my own use, and in the hope that others may get some help from it.  I pass my thanks to the gnuplotting blog for some useful tips to help me get started, and the nice idea that having less prominent grids and tick labels makes things easier to read.

The figure is above, and the necessary code follows, with commentary below:
set term 'pngcairo' font 'Helvetica,12'
set out 'cites.png'

set style line 1 lc rgb '#440000' lt 1
set style line 2 lc rgb '#882200' lt 1
set style line 3 lc rgb '#bb6622' lt 1
set style line 4 lc rgb '#ffcc66' lt 1

set style line 11 lc rgb '#808080' lt 1
set border 3 back ls 11
set tics nomirror
set xtics 1,1.0,25.0 offset -1.5,0.0
set style line 12 lc rgb '#808080' lt 0 lw 1
set grid back ls 12

set style fill solid 1.0 border rgb 'grey30'

set ylabel 'citations'
set xlabel 'Paper Index'
set xrange [0:25]

plot 'cite-data' u ($0+0.5-1.5*bw):1:(bw) w boxes ls 1 t 'Google Scholar', \
     'cite-data' u ($0+0.5-0.5*bw):2:(bw) w boxes ls 2 t 'Scopus', \
     'cite-data' u ($0+0.5+0.5*bw):3:(bw) w boxes ls 3 t 'ISI', \
     'cite-data' u ($0+0.5+1.5*bw):4:(bw) w boxes ls 4 t 'Journal site'
I used the pngcairo terminal, in the belief that it is better than the png terminal, but I should really investigate that.

The block of "set style line" commands is used just to set the colour of the filling of the bars in the bar chart.  If you follow the link to the blog post from my other blog that I mentioned above, you'll see that I didn't have those colour-changing lines, and used the default colours instead.  I'm not sure which I prefer, but this at least shows how you can change them.

The next block sets up the border, tick marks and labels and the grid.  Line styles 11 and 12 are used to set the border and grid to be grey rather than black, to accentuate the actual data.  I used the set tics command to define user tick marks.  I set up the data file to have no abscissa column, so it will be plotting it just by the ordinal number, and a little playing with the offset enabled me to get the numbers in the right place.

The set style fill line is needed to fill in the bars.  If you don't set this, you'll just have box outlines, so if that's what you want, don't have this line.  You can specify the border to be a different colour from the fill, as indicated.

The last section sets up the bars.  I have four bars to show per ordinate.  I decided to make each one take up a fraction of 0.15 the width of the space available for each data point.  The bw=0.15 sets up a variable to store this.  In the plot command, the $0 specifies the ordinate for each point, since I don't provide x-value points in the cite-data file.  The calculations such as $0+0.5-1.5*bw specify the centre of the point at which the bar appears, and the (bw) the width.

If you want to play with this, here is the file cite-data referred to in the gnuplot script.