5

Related to how to set label and line the same color in gnuplot - but not quite the same... Using this code:

set style line 1 linetype 1 linewidth 2 pointtype 3 linecolor rgb "aquamarine"
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue"
set style line 3 linetype 1 linewidth 2 pointtype 3 linecolor rgb "coral"

set xrange [0:3]
set yrange [0:3]

# function to get style index for coloring:
getCol(x) = (x==0)?1:((x==1)?2:3);

plot \
  '-' using ($1+0.5):($2+0.5):(getCol($2)) with impulses \
    lc variable notitle, \
  "" using ($1+0.5):($2+0.5):(stringcolumn(2)):(getCol($2)) with labels \
    textcolor variable point linestyle 1 pointtype 7 lc variable \
    font ",6" offset character 1.0,character 1.0  notitle
0 0
1 1
1.5 1
2 2
e
0 0
1 1
1.5 1
2 2
e

... I get this output:

gnuplot-out

It looks like everything is fine for coloring the impulses - however for the labels, the approach seemingly works - but it's as if the colors are read from a different index ?!

So, how do I get the points and the labels to have the same variable color, using the same function - as the impulse lines? This is on gnuplot 4.6 patchlevel 1...

Community
  • 1
  • 1
sdaau
  • 36,975
  • 46
  • 198
  • 278
  • This issue is fixed in the newly released 4.6 patchlevel 4. Just add the line `set style increment user` before plotting, and you'll get the expected result (the same as in [my answer](http://stackoverflow.com/a/18368864/2604213)). – Christoph Oct 09 '13 at 12:13

1 Answers1

3

The color index for the impulses is used to select a line style, while for the labels the line type is used. This behaviour is still present in the current development version. According to the documentation, the behaviour for the labels is correct. help linecolor variable says: "lc variable tells the program to use the value read from one column of the input data as a linetype index... Text colors can be set similarly using tc variable". Bug, feature or wrong documentation?

As a workaround you can use lc palette and an appropriately defined palette:

set style line 1 linetype 1 linewidth 2 pointtype 3 linecolor rgb "aquamarine"
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue"
set style line 3 linetype 1 linewidth 2 pointtype 3 linecolor rgb "coral"

set palette defined (1 "aquamarine", 2 "blue", 3 "coral")
unset colorbox
set xrange [0:3]
set yrange [0:3]

# function to get style index for coloring:
getCol(x) = (x==0)?1:((x==1)?2:3)

unset key
plot \
  '-' using ($1+0.5):($2+0.5):(getCol($2)) with impulses lc variable, \
  '' using ($1+0.5):($2+0.5):(stringcolumn(2)):(getCol($2)) with labels \
    tc palette point ls 1 pt 7 lc palette offset 1,1
0 0
1 1
1.5 1
2 2
e
0 0
1 1
1.5 1
2 2
e

This gives the result: enter image description here

I hope that workaround works also in your real case. If for some reason you can't redefine the palette, you can use lc rgb variable, in which case the last column must be an integer representation of the respective rgb color:

set style line 1 linetype 1 linewidth 2 pointtype 3 linecolor rgb "aquamarine"
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue"
set style line 3 linetype 1 linewidth 2 pointtype 3 linecolor rgb "coral"

set xrange [0:3]
set yrange [0:3]

rgb(r,g,b) = 65536 * int(r) + 256 * int(g) + int(b)
rgb1 = rgb(2, 215, 245) # something like aquamarine
rgb2 = rgb(0, 0, 255)
rgb3 = rgb(245,140,2)
getCol(x) = (x==0)?rgb1:((x==1)?rgb2:rgb3)

unset key
plot \
  '-' using ($1+0.5):($2+0.5):(getCol($2)) with impulses \
    lc rgb variable, \
  '' using ($1+0.5):($2+0.5):(stringcolumn(2)):(getCol($2)) with labels \
    tc rgb variable point ls 1 pt 7 lc rgb variable offset 1,1
0 0
1 1
1.5 1
2 2
e
0 0
1 1
1.5 1
2 2
e

You may only need to tweek the rgb values.

However, I will bring up this discussion on the gnuplot mailinglist to see if this is really a bug, or whatever.

Christoph
  • 47,569
  • 8
  • 87
  • 187
  • Many thanks @Christoph - great answer; by accident, I discovered this same `set palette`/`unset colorbox` workaround, so it works in my test case as well. But it's great to have learned about the `lc rgb variable` approach. Thanks again - cheers! – sdaau Aug 21 '13 at 22:45
  • @sdaau It takes some time to discover all the different possibilities which `gnuplot` offers to do the very same things `:)` Good to hear that it helped you. – Christoph Aug 21 '13 at 22:47