asylumax
asylumax

Reputation: 845

Is there a simpler way to create gnuplot filled curves for time data when data is only recorded on changes?

It is easy to create a filledcurve plot when time series data is at small regular intervals, and data changes infrequently.

But what if data is recorded only on changes?

In order to make a shaded plot for when data is '1', there needs to be another point before the change in value (the ZZ.999 values):

# test shaded
#
t, v1
0, 0
9.999, 0
10, 1
14.999, 1
15, 0
29.999, 0
30, 1
44.999,1
45, 0
59.999,0
60, 1
64.999, 1
65, 0

This will give you: enter image description here

This is done by:

set datafile separator "," 
set datafile commentschar '#'
set key autotitle columnhead # use the first line as title for data

fileused = 'shaded.csv'

set xtics 5
set xrange[0:90]

set term qt size 800, 400

plot fileused using 1:2 with filledcurves

Is there a way to get the same plot, without having the ZZ.999 datapoints in the dataset, so you only need the data below?

0, 0
10, 1
15, 0
30, 1
45, 0
60, 1
65, 0

Upvotes: 1

Views: 64

Answers (2)

Ethan
Ethan

Reputation: 15118

There are several plot styles that might suit. The simplest is probably plot with fillsteps.

$DATA << EOD
0, 0
10, 1
15, 0
30, 1
45, 0
60, 1
65, 0
EOD

set datafile separator comma
set xtics 5
set xrange[0:90]
set yrange[0:1.5]

set style fill solid
plot $DATA using 1:2 with fillsteps

enter image description here

Upvotes: 2

theozh
theozh

Reputation: 26123

Ethan's solution with fillsteps is clearly the simplest solution for your data and purpose. Revisiting this answer, I noticed that from gnuplot 6.0.2 on empty lines are apparently handled differently, e.g. for histeps plotting style. So, I tried to plot fillsteps with some empty lines in the data, and apparently it does not plot some blocks in the graph anymore. However, this is probably as intended by the program, since lines plots will also be interrupted by empty lines.

In order to ignore empty (and invalid) lines, I ended up with the following script. I used multiplot to illustrate the differences to fillsteps. If you want the line at y=0 to disappear, set style fill solid noborder.

Script:

### plot with fillsteps having empty lines in the data
reset session

$Data <<EOD
0   0
10  1

15  0

30  1

45  0
60  1
65  0
EOD

set style fill solid
set offset 0,0,1,0.1
set key noautotitle

getXY(colX,colY) = (valid(colX) && valid(colY) ? (y0=y1,y1=column(colY), \
                    x0=x1,x1=column(colX)) : 0, (x1+x0)/2)

set multiplot layout 2,1
    plot $Data u 1:2 w fillsteps fc rgb 0xff7777

    plot x0=y0=x1=y1=NaN $Data u (getXY(1,2)):(y0):(x1-x0) w boxes fc rgb 0x77ff77
unset multiplot
### end of script

Result:

enter image description here

Upvotes: 1

Related Questions