## gnuplot: different color on different segments of a curve

I’m not sure what’s the proper description, but here’s a task at hand and how it is done in gnuplot:

suppose I have a datafile with 3 columns. I can plot columns 1 and 2 as a curve:

gnuplot> p 'datfile' u 1:2

In my problem, the 3rd column indicates whether the entry is “localized on the left side”, which will have `abs($3)>1`, or the “right side”, having `abs($3)<1`. I want the curve to be plotted in one color (say red) for the “left side” segment(s), and another color (say green) for the “right side” segment(s)

somewhere in gnuplot’s manual, it says it will silently drop data if they are unplottable, and gives an example that `1/0` is unplottable (division by zero). so here’s the idea:

define the “above” and “below” function:

gnuplot> above(x,a) = x > a ? 1 : 1/0

gnuplot> below(x,a) = x < a ? 1 : 1/0

then I can use them to pick out the left/right segments:

gnuplot> p '' u 1:($2*above(abs($3),1)) w l lt rgb 'red' lw 2 tit "localized to the right"

gnuplot> rep u 1:($2*below(abs($3),1)) w l lt rgb 'green' lw 2 tit "localized to the left"

there is a minor problem: for data files, the segments with different colors are always separated by a small gap b/c neither `above` nor `below` would cover that gap. Haven’t thought about how to solve this yet, probably a clever redef of `above` and `below` could do the job.

## Leave a Reply