user3778424
user3778424

Reputation: 121

Gnuplot conditional plotting

I need to use gnuplot to plot wind direction values (y) against time (x) in a 2D plot using lines and points. This works fine if successive values are close together. If the values are eg separated by 250 degrees then I need to have a condition that checks the previous y value and does not draw a line joining the two points. This condition occurs when the wind dir is in the 280 degrees to 20 degrees sector and the plots are messy eg a north wind. AS the data is time dependent I cannot use polar plots except at a specific point in time. I need to show change in direction over time.

Basically the problem is:

plot y against x ; when (y2-y1)>= 180 then break/erase line joining successive points Can anyone give me an example of how to do this?

A sample from the data file is:

2014-06-16 16:00:00 0.000 990.081 0.001 0.001 0.001 0.001 0.002 0.001 11.868 308 002.54 292 004.46 00 
2014-06-16 16:10:00 0.000 990.047 0.001 0.001 0.001 0.001 0.002 0.001 11.870 303 001.57 300 002.48 00 
2014-06-16 16:20:00 0.000 990.014 0.001 0.001 0.001 0.001 0.002 0.001 11.961 334 001.04 314 002.07 00 
2014-06-16 16:30:00 0.000 990.014 0.001 0.001 0.001 0.001 0.002 0.001 11.818 005 001.18 020 002.14 00 
2014-06-16 16:40:00 0.000 990.014 0.001 0.001 0.001 0.001 0.002 0.001 11.725 332 001.14 337 002.26 00

and I want to plot column 12 vs time.

Upvotes: 2

Views: 3721

Answers (3)

theozh
theozh

Reputation: 25684

There is absolutely no need for awk. You can simply "interrupt" the lines by using variable color for the line. For gnuplot<5.0.0 you can use 0xffffff=white (or whatever your background color is) as linecolor and the line will hardly be visible. For gnuplot>=5.0.0 you can use any transparent color, e.g. 0xff123456, i.e. the line is really invisible.

Data: SO24425910.dat

2014-06-16 16:00:00   330
2014-06-16 16:10:00   320
2014-06-16 16:20:00   310
2014-06-16 16:30:00   325
2014-06-16 16:40:00   090
2014-06-16 16:50:00   060
2014-06-16 17:00:00   070
2014-06-16 17:10:00   280
2014-06-16 17:20:00   290
2014-06-16 17:30:00   300

Script: (works for gnuplot>=4.4.0, March 2010)

### conditional interruption of line
reset

FILE = "SO24425910.dat"

set key noautotitle
set yrange[0:360]
set ytics 90
set grid x
set grid y
set xdata time
set timefmt "%Y-%m-%d %H:%M"
set format x "%H:%M"

set multiplot layout 2,1

    plot y1=NaN FILE u 1:(y0=y1,y1=$3):(abs(y1-y0)>=180?0xffffff:0xff0000) w l lc rgb var

    plot y1=NaN FILE u 1:(y0=y1,y1=$3):(abs(y1-y0)>=180?0xffffff:0xff0000) w l lc rgb var, \
                  '' u 1:3 w p pt 7 lc rgb "red"

unset multiplot
### end of script

Result:

enter image description here

Upvotes: 0

user3778424
user3778424

Reputation: 121

In order to break the joining lines two conditional statements must be fulfilled and BOTH must include the newline statement printf("\n"):

plot '< awk ''{y1 = y2; y2 = $12; if (NR > 1 && y2 - y1 >= 180) printf("\n") ; if (NR > 1 && y2 -y1 <= 0) printf("\n"); print}'' /Desktop/plotdata.txt' using 1:12 with linespoints

Upvotes: 1

Christoph
Christoph

Reputation: 48390

You can insert a filtering condition in the using statement and use a value of 1/0 if the condition is not fullfilled. In that case this point is not connect to others:

set timefmt '%Y-%m-%d %H:%M:%S'
set xdata time
unset key

y1 = y2 = 0
plot 'data.dat' using 1:(y1 = y2, y2 = $12, ($0 == 0 || y2 - y1 < 180) ? $12 : 1/0) with lines,\
     'data.dat' using 1:12 with points

With your data sample and gnuplot version 4.6.5 I get the plot:

enter image description here

Unfortunately, with this approach you cannot categorize lines, but only points and also the line following the 1/0 point aren't drawn.

A better approach would be to use awk to insert an empty line when a jump occurs. In a 2D-plot points from different data blocks (separated by a single new line) aren't connected:

set timefmt '%Y-%m-%d %H:%M:%S'
set xdata time
unset key

plot '< awk ''{y1 = y2; y2 = $12; if (NR > 1 && y2 - y1 >= 180) printf("\n"); print}'' data.dat' using 1:12 with linespoints

Upvotes: 2

Related Questions