Search code examples
gnuplotrgb

GNUPlot: cannot plot 2D scatterplot with colors specified


I'm attempting to make a 2D scatterplot using advice that I received Cannot get labels to color in GNUPlot

which led me to write:

$DATA << EOD
3.12960004005165    2.67469663590573    0.438527882947657
3.54159282682062    1.48608712150729    3.45059090334765
1.41832139944261    1.09739773669576    4.7851179962913
0.37050714994141    4.51679511956513    2.99273024822522
1.33417336450785    2.60931115605578    3.49852244355505
4.68608825227413    4.50263530942483    0.708639941290272
EOD
set datafile separator "\t"
set xlabel 'x'
set ylabel 'y'
set title 'title'
set terminal svg
set output 'scatterplotx_y.svg'
set key off
myColor(col) = int("0x".strcol(col)[3:])
plot $DATA using 1:2:(myColor(3)) pt 7

I'm using gnuplot 5.2 patchlevel 8

I've also tried

plot $DATA u 1:2:3 with lines palette

suggested by Line plot in GnuPlot where line color is a third column in my data file? but that doesn't work

plot $DATA u 1:2:3 with points color $3

I'm using a palette now:

set palette defined (0 "red", 0.3 "orange", 0.625 "green", 0.85 "blue", 1 "dark-violet")

My question:

How can I plot the color from red to violet as determined by a number in [0,1], here the 3rd column? where red would be 0 and 1 would be violet, with all of the colors of the rainbow in the middle?


Solution

  • You are missing a keyword in the plot command to tell the program what the third column is to be used for.

    plot $DATA u 1:2:3 with lines linecolor palette
    

    Otherwise the program ignores the extra column because it doesn't know whether it is a z coordinate, a point size, or various other possibilities.

    Actually there are further choices within the linecolor palette option. It could be linecolor palette z or linecolor palette fraction or linecolor palette cb.

    • The default is the first one palette z. It autoscales the values to use the entire palette range for the data in this plot.
    • palette fraction means the 3rd column is a number between 0 and 1 that selects a color that far into the palette range.
    • palette cb means the value in the 3rd column refers to a separate color range controlled by set cbrange [min:max].