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