Wednesday 15 July 2009

Grid with multiple colours

A couple of days ago, I very badly needed a graph on which all four axes were used, and in addition to that, I needed a grid on the graph. Now, the problem is, if you just plot a grid, it will be hard to follow which grid line corresponds to which axis. So, I decided to colour both the grid, and the axes, and use the same colour for corresponding grid lines and axes. Well, this is what I decided, but then it turned out to be a rather hard nut to crack. (In gnuplot 4.3, the situation is a bit better, I will discuss that tomorrow.) I have found a work-around, however, so if you are interested, you can find the details below. But let us see the graph first!



The standard way of defining a grid is a line similar to this:
set grid ytics lt 0 lw 1 lc rgb "#880000"


First, we define where we want to have the grid (everywhere, where there is an ytics), then the linetype, the line width, perhaps, and the colour. The problem is that when you issue this command multiple times, for some funny reason, only the last colour setting will take effect, i.e., if after the line above, you happened to have
set grid y2tics lt 0 lw 1 lc rgb "#008800"

then gnuplot would place the grid lines at y2tics all right, but the grid lines at ytics would also be green, and this is not what we want. The way out of this difficulty is to trick gnuplot into thinking that we have more than one plot, and re-set the grid before each new plot. Of course, we don't actually want to plot anything after the first one, therefore, we will just plot 1/0. It is a vicious way to deceive gnuplot, but it does the trick. Now, here is the script

reset
h=4.136e-15 # eV s
hbar=6.57e-16 # eV s
c=3e17  # nm/s

set multiplot
unset key
set xlabel 'Wavelength [nm]' tc rgb "#0000ff"
set x2label 'Temperature [K]'
set ylabel 'Energy [eV]' tc rgb "#880000"
set y2label 'dE/d{/Symbol l} [meV/nm]' tc rgb "#008800"
set xrange [200:2000]
set x2range [0:10]
set yrange [0:6]
set y2range [1:-7]
set y2tics 1,-1,-7
set ytics nomirror tc rgb "#880000"
set y2tics tc rgb "#008800"
set xtics nomirror tc rgb "#0000ff"
set x2tics
set mxtics 2
set mytics 2
set grid xtics lt 0 lw 1 lc rgb "#0000ff"

plot h*c/x w l lc rgb "#880000", -1000*h*c/x/x axes x1y2 w l lc rgb "#008800"

unset grid
set xlabel " "
set xtics format " "
set x2tics format " "
set x2label " "
set ylabel " "
set y2label " "
set grid x2tics lt 0 lw 1 lc rgb "#000000"
plot 1/0

unset grid
set grid ytics lt 0 lw 1 lc rgb "#880000"
plot 1/0

unset grid
set grid y2tics lt 0 lw 1 lc rgb "#008800"
plot 1/0

unset multiplot


First, we define a couple of constants, and then set up the axis labels. Note that when doing so, we assign a colour to each axis, so that we will know which axis the plot belongs to. Of course, we could do this with arrows, but since we want to have coloured grids, it is better to do it in this way. Next, we set the various ranges, and then set up the first grid line, which will be drawn at every xtics, and in blue. (Blue was the label colour of x.) After plotting the curves, we unset the grid, and we also take off the labels and ticlabels. Note that when doing so, we, in fact, re-set the labels, but with an empty string. The reason for this is that, if we simply take off the labels, then the margins of the graph will change, and when we plot our next graph (which is empty, but we still need the grid), it would be of a different size, and would not align with our original plot. Having re-labelled the graph, we re-set the grid, this time putting the lines on x2tics in black, then on ytics in a dark shade of red, and finally, on y2tics, in dark green. At the very end, we unset the multiplot, i.e., escape from the whole plot.
This is a bit dirty, but if you are pressed for it, it should still be OK. Next time, I will show the easy route, provided you have gnuplot 4.3.

7 comments:

  1. Thanks for a very helpful post.

    ReplyDelete
  2. Hello,

    I have question when using splot with gnuplot.
    The issue am facing is, i have data file with point coordinates in the two first colums and value in the last column. But i do not have the same values in any column.

    The data file look like:

    x1 y1 val
    x2 y2 val
    x3 … val
    x4 yn val

    x5 y5 val
    x6 y6 val
    x7 … val
    xn yn val

    ……

    I tried to use splot with gnuplot but it didn’t plot all value in val, juste some of them. For exemple i have the first value for val is 35 and the last 64 et gnuplot with splot , just plot from 48 to 60. I changed the zrange, but not helping.

    Thanks in advance.

    ReplyDelete
  3. Hi,

    maybe you can help me.

    I'm using the fig-terminal in gnuplot but unfortunately the color option rgb #HEX does not work in ths terminal. Is there a way to select specific colors (and gray lines!) for the fig-terminal?

    Thanks!

    ReplyDelete
  4. set pm3d
    set palette rgb 33,13,10
    set view map
    splot 'file.xyz' with points palette pt 5 ps 0.5

    Some explanation:
    - set pm3d activates a package that allows coloring points by value;
    - the palette command will create a much used color scale: blue-green-yellow-red;
    - set view map will disable 3d view, but show a map view instead;
    - splot plots the points in the file;
    - "pt 5" specifies pointtype 5, meaning a triangle;
    - "ps 0.5" specifies pointsize 0.5, meaning so small that it almost looks like a filled dot.

    http://www.unix.com/unix-advanced-expert-users/43236-gnuplot-question-plotting-3d-data-map-view.html

    ReplyDelete
  5. I am trying to use multiplot to draw a geographical boundary (using plot command) and then I wish to overlay a 3d plot using splot. This seems nearly impossible. Can eny one help.

    ReplyDelete
  6. Hi gipssss,

    Could you post a link to a graph? I am not sure I understand the problem. In fact, I am quite sure I don't...
    Cheers,
    Zoltán

    ReplyDelete