z·eeki·sh

tech log on gentoo, linux, and random stuff

gnuplot: different color on different segments of a curve

leave a comment »

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.

Attached is some real world example.
colored segments

Advertisements

Written by zsh

November 29, 2010 at 12:41 am

Posted in /usr/bin

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: