2

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.

2
  • Why not you filter lines using awk utility and keep only required lines to be given as input to gnuplot Commented Jun 26, 2014 at 8:40
  • Thanks Stauz - I need to retain all values, no data can be omitted. Its just the lines connecting points that are the problem. Commented Jun 26, 2014 at 8:46

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you Cristoph - when I try your suggestion I get 'undefined variable: y2'...
Data sample : 2014-06-16 00:10:00 0.000 990.147 0.001 0.001 0.001 0.001 0.002 0.001 11.479 248 004.16 257 004.81 00 2014-06-16 00:20:00 0.000 990.147 0.001 0.001 0.001 0.001 0.002 0.001 11.509 247 004.04 241 005.06 00 2014-06-16 00:30:00 0.000 990.180 0.001 0.001 0.001 0.001 0.002 0.001 11.506 258 004.86 259 006.14 00 2014-06-16 00:40:00 0.000 990.147 0.001 0.001 0.001 0.001 0.002 0.001 11.505 253 003.67 256 005.16 00 2014-06-16 00:50:00 0.000 990.081 0.001 0.001 0.000 0.001 0.002 0.001 11.489 243 003.64 254 004.41 00 I am plotting column 12 vs time
Here is more data with y (Column 12) crossing 360 degrees :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
Ok, see my edit. I think the awk variant should work best. I included the data samples in your question. If you do this by yourself, just do copy&paste, mark the data and press Ctrl+K to format it properly.
I am trying awk as you suggested - input : plot "<awk '{y1 = y2; y2 = $12; if (NR > 1 && y2 - y1 >= 180) printf("\n"); print}" '/Desktop/plotdata.txt' with linespoints ......This gives me err msg as follows : plot "<awk '{y1 = y2; y2 = $12; if (NR > 1 && y2 - y1 >= 180) printf("\n"); print}" '/Desktop/plotdata.txt' with linespoints ^ invalid character \ (in printf("\n");)
|
1

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

1 Comment

To have it a bit shorter, you can use the condition if (NR > 1 && (y2-y1 >= 180 || y2-y1 <= 0)) printf("\n") :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.