Gnuplotting

Create scientific plots using gnuplot

July 16th, 2012 | 10 Comments

Sometimes it can be helpful to visualize a third dimension by the color of the line in the plot. For example in Fig. 1 you see a logarithmic sweep going from 0 Hz to 100 Hz. Here the frequency is decoded by the color of the line.

Logarithmic sweep

Fig. 1 A logarithmic sweep ranging from 0 Hz to 100 Hz and decoding the frequency with the line color (code to produce this figure, data)

This can be easily achieved by adding a lc palette to the plot command, which uses the values specified in the third row of the data file.

plot 'logarithmic_sweep.txt' u 1:2:3 w l lc palette

The palette can be defined as shown in the Multiple lines with different colors entry. But it can be set in a more easy way, by only setting the start and end color and calculating the colors in between. Therefore, we are picking the two hue values in GIMP (the H entry in Fig. 2 and Fig. 3) for the starting and ending color.

Picking the first hue value

Fig. 2 Picking the HSV value corresponding to the given color of #09ad00.

Picking the second hue value

Fig. 3 Picking the HSV value corresponding to the given color of #0025ad.

These colors are then used to specify the palette in HSV mode. The S and V values can also directly be seen in GIMP.

# start value for H
h1 = 117/360.0
# end value for H
h2 = 227/360.0
# creating the palette by specifying H,S,V
set palette model HSV functions (1-gray)*(h2-h1)+h1,1,0.68

10 Comments

  1. Giambattista says:

    Excellent! This was extremely useful, thanks!

  2. Liam says:

    Hello.
    I am new to GNUplot and I was wondering, is it necessary to setup 3 columns of data. I am trying to setup an xy plotted curve using the color gradient similar to the above graph. Low values will be green, while high values will be red. Is there anyway to set the start and end values for just the y-values?
    Thanks

  3. hagen says:

    Yes, you can just reuse the y data for the third column:

    plot 'logarithmic_sweep.txt' u 1:2:2 w l lc palette
    

    Or if you want to do some manipulations of that data:

    plot 'logarithmic_sweep.txt' u 1:2:(sqrt($2)) w l lc palette
    
  4. Liam says:

    Thanks! This really helped! I am a student who is helping a friend with a report and this made things so much easier.

  5. Vremo says:

    I was wondering, could i have three curves with lc palette with three different dashing styles one the same plot? It doesn’t seem to work.

  6. hagen says:

    The problem could be your terminal, because dashing is not working with all terminals at the moment.

    If you try the following changes to the code from above you will get a postscript file with two different dashed lines

    set terminal postscript
    set output 'sweep.ps'
    plot 'logarithmic_sweep.txt' u 1:(0.4*$2-0.5):3 w l lc palette lt 1, \
         ''                      u 1:(0.4*$2+0.5):3 w l lc palette lt 3
    
  7. irrelevant says:

    How is it possible to make these palettes transparent in order to distinguish and see highly overlapping functions or data?

  8. hagen says:

    Transparent line colors are not possible at the moment, but the development version of gnuplot have them:
    http://sourceforge.net/tracker/?func=detail&aid=3286640&group_id=2055&atid=302055

  9. Gerson says:

    I’m not able to use dashed lines and lc palette at the same time.

    It all works fine if I only have a small set of points, and each straight line segment gets dashed. But if I have many points as a smooth curve, I guess gnuplot makes dashed lines between segmetns of the same color… which are tiny, and leads to continuous curve.

    Can you try this with our logarithmic_sweep.txt? Pretty much the same code you have above, but in term wxt or epslatex.

  10. hagen says:

    I get the same result. Dashed lines in gnuplot is in general a little buggy. It is not the first time that it didn’t worked.

Leave a Reply