|
From: theozh <th...@gm...> - 2018-06-02 05:14:28
|
Hello,
I am trying to create a plot with background color depending on a datafile. The datafile basically just contains 0s and 1s for "off" and "on". When it is "on" the background should be e.g. a light green and should span the whole y-range of the plot.
I tried with fillstep and calculate the ymin and ymax using the 0s and 1s from the data file and GPVAL_Y_MIN and GPVAL_Y_MAX. That's why I apparently have to use "replot" to get the actual (autoscaled) values.
The script below seem to work in general but for some strange reason not always. Sometimes the GPVAL_Y_MIN and GPVAL_Y_MAX values do not correspond to the y-axis minimum and maximum.
Furthermore, the fillstep fills always to 0, not from ymin to ymax.
Maybe I am just thinking too complicated and there is a function to do this much simpler?
Any ideas? Thank you.
I put the following script into a loop so that you can try several times and see that it sometimes works sometimes not...
# gnuplot script: local background depending on data
reset
$Data <<EOD
1 0
2 0
3 1
4 1
5 0
6 0
7 0
8 1
9 1
10 1
11 1
12 0
13 0
14 0
15 0
EOD
set colorsequence classic
do for [i=1:100] {
A = rand(0)*100
B = rand(0)*100
plot A*sin(x) + B
set label 1 sprintf("A: %g\nB: %g\nGPVAL_Y_MIN: %g\nGPVAL_Y_MAX: %g",A,B,GPVAL_Y_MIN,GPVAL_Y_MAX) at screen 0.5,0.5 noenhanced
replot $Data u 1:((GPVAL_Y_MAX - GPVAL_Y_MIN)*$2 + GPVAL_Y_MIN) w fillsteps fs transparent solid 0.1
pause -1 "Press Enter to next plot"
}
# end gnuplot script
|
|
From: Ethan A M. <eam...@gm...> - 2018-06-02 05:47:24
|
On Saturday, 02 June 2018 07:14:17 theozh wrote:
> Hello,
> I am trying to create a plot with background color depending on a datafile. The datafile basically just contains 0s and 1s for "off" and "on". When it is "on" the background should be e.g. a light green and should span the whole y-range of the plot.
> I tried with fillstep and calculate the ymin and ymax using the 0s and 1s from the data file and GPVAL_Y_MIN and GPVAL_Y_MAX. That's why I apparently have to use "replot" to get the actual (autoscaled) values.
> The script below seem to work in general but for some strange reason not always. Sometimes the GPVAL_Y_MIN and GPVAL_Y_MAX values do not correspond to the y-axis minimum and maximum.
> Furthermore, the fillstep fills always to 0, not from ymin to ymax.
> Maybe I am just thinking too complicated and there is a function to do this much simpler?
> Any ideas? Thank you.
My take is
set linetype 101 lc bgnd
set linetype 102 lc "seagreen"
set boxwidth 1.0
set yrange [0:*]
BIGNUM = 1.e5
plot $Data using ($1+0.5):(BIGNUM):($2==0?101:102) with boxes lc var noautoscale, \
whatever-the-foreground-plot-is
If ymin < 0 you would have to tweak this a bit.
Maybe draw the boxes twice, once with +BIGNUM once with -BIGNUM.
Or use boxxy, but that requires a more complicated using spec.
Ethan
>
> I put the following script into a loop so that you can try several times and see that it sometimes works sometimes not...
>
> # gnuplot script: local background depending on data
> reset
> $Data <<EOD
> 1 0
> 2 0
> 3 1
> 4 1
> 5 0
> 6 0
> 7 0
> 8 1
> 9 1
> 10 1
> 11 1
> 12 0
> 13 0
> 14 0
> 15 0
> EOD
> set colorsequence classic
>
> do for [i=1:100] {
> A = rand(0)*100
> B = rand(0)*100
> plot A*sin(x) + B
> set label 1 sprintf("A: %g\nB: %g\nGPVAL_Y_MIN: %g\nGPVAL_Y_MAX: %g",A,B,GPVAL_Y_MIN,GPVAL_Y_MAX) at screen 0.5,0.5 noenhanced
> replot $Data u 1:((GPVAL_Y_MAX - GPVAL_Y_MIN)*$2 + GPVAL_Y_MIN) w fillsteps fs transparent solid 0.1
> pause -1 "Press Enter to next plot"
> }
> # end gnuplot script
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> gnuplot-info mailing list
> gnu...@li...
> Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-info
|
|
From: theozh <th...@gm...> - 2018-06-02 21:18:35
|
Thank you Ethan,
ok, I haven't thought of boxes... probably the easiest workaround.
One way is certainly plotting boxes to +BIGNUM and -BIGNUM if you know for sure your data will be below this value.
The modified script below uses "replot" and GPVAL_Y_MAX and GPVAL_Y_MIN and works without BIGNUM. It also accounts for the fact that $Data column1 might also consist of other x-steps than 1.
However, when $Data column2 is 0="off" there is still be a hardly visible line at y=0. Actually, if I draw a box with height 0 I would expect to see absolutely nothing.
Well, this line you can avoid by changing the color to background using the ternary operator, as you did.
Thanks again, Theo.
PS.
minor detail still to fix: if $Data Col2 starts with 0, the key in the legend will have bgnd color (="invisible") instead of green color.
# local background depending on data
reset
$Data <<EOD
1 0
3 0
5 1
7 1
11 0
15 0
16 0
20 1
21 1
22 1
23 1
25 0
26 0
27 0
28 0
EOD
set colorsequence classic
do for [i=1:100] {
A = rand(0)*100
B = rand(0)*100
set linetype 101 lc bgnd
set linetype 102 lc "green"
set boxwidth 1.0 relative
plot \
$Data u 1:(A*sin($1)+B) w l lt 1
# reuse the GPVAL_Y_MAX and GPVAL_Y_MIN values
replot \
$Data u 1:(GPVAL_Y_MAX*$2):($2==0?101:102) w boxes lc var fs transparent solid 0.1 noborder t "shutter open" noautoscale, \
$Data u 1:(GPVAL_Y_MIN*$2):($2==0?101:102) w boxes lc var fs transparent solid 0.1 noborder notitle noautoscale, \
pause -1 "Press Enter to next plot"
}
# end gnuplot script
|