Plot with shaded regions between lines - gnuplot

I have the 2 files from either of them I'll plot a point line using the following code:
set terminal postscript eps color solid font "Helvetica, 22"
set size ratio 0.625
set output "example.eps"
set key right top
plot "traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
Either line forms a region together with x axis and the regions formed by both lines overlaps. I was wondering how may I shade the overlapping parts.
Thanks!
(The files used are as follows)
File 1
1 66.660000
2 47.830000
3 39.270000
4 27.940000
5 24.990000
6 27.930000
7 32.060000
8 43.650000
9 70.470000
10 73.430000
11 87.690000
12 111.790000
13 122.170000
14 114.930000
15 111.620000
16 109.330000
17 121.370000
18 118.600000
19 132.890000
20 132.480000
21 148.360000
22 152.260000
23 140.510000
24 99.120000
File 2
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0.121933
10 1.81455
11 2.25622
12 2.67994
13 2.87834
14 2.53149
15 1.29541
16 0.57571
17 0.0883007
18 0
19 0
20 0
21 0
22 0
23 0
24 0

You can use the filledcurves plotting style. For that all data must be contained in one file. You can combine the files on-the-fly e.g. with paste. For a platform-independent solution with python look e.g. this answer.
With filledcurves you can also distinguish between above and below in order to use different colors:
set key right top
set autoscale xfix
set xtics 4
plot "< paste traffic.txt solar.txt" using 1:2:($4*100) with filledcurves below lc rgb "#ffaaaa" t '', \
"" using 1:2:($4*100) with filledcurves above lc rgb "#aaaaff" t '',\
"traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
The result with 4.6.4 is:
To shade only the region where the two curves overlap, you need a workardound. First shade the region between the narrower curve and the x1 axis, and then overwrite parts of that shading with white:
set autoscale xfix
set xtics 4
plot "< paste traffic.txt solar.txt" using 1:($4*100) with filledcurves x1 lc rgb "#ffaaaa" t '', \
"" using 1:2:($4*100) with filledcurves below lc rgb "white" t '',\
"traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
This gives:

Related

gnuplot: how to set the different fill and border colors for `with points`?

I would like to achieve a similar effect in gnuplot.
Here is what I tried:
unset key
set style line 11 lc rgb '#808080' lt 1
set border 3 ls 11
set tics nomirror
set grid
set style line 1 lc rgb '#808080' pt 9 ps 3
set style line 2 lc rgb '#808080' pt 20 ps 3
set style line 3 lc rgb '#BD3828' pt 7 ps 3
set yrange [4:9]
$data << EOD
5 5.1
5.3 6.8
6 6
EOD
$data2 << EOD
5 5
7 7
8 6
EOD
$data3 << EOD
5.5 7
6 6
7 7.1
EOD
plot $data u 1:2 w points ls 1, $data2 u 1:2 w points ls 2, \
$data3 u 1:2 w points ls 3
As we can see, points can be overlapped. Then how can we darken the overlap areas?
A possible solution is to set transparency (e.g., lc rgb '#80808080'), but it will also make both border and filling transparent. So how to set the different fill and border colors for with points?
Another solution is to use set object, but we need to do more work to read data from files.
I think the closest you could come to what you describe is to draw the points in two passes.
First pass: draw using a point type that produces only the outlines (point types N = 4 6 8 10 12 ...).
Second pass: draw using the corresponding point type N+1 that produces only the interior, using the same color but adding an alpha channel value to make it partially transparent.
set print $RAND1
do for [i = 1:50] { print rand(0), rand(0) }
unset print
set print $RAND2
do for [i = 1:50] { print rand(0), rand(0) }
unset print
set pointsize 4
plot $RAND1 with points pt 8 lc rgb "#00b8860b", \
'' with points pt 9 lc rgb "#AAb8860b", \
$RAND2 with points pt 6 lc rgb "#00c04000", \
'' with points pt 7 lc rgb "#AAc04000"

gnuplot: How to create colored grid regions, not just colored grid lines?

I am using "gnuplot 5.2 patchlevel 2". I am trying to create background grid colored-columns or colored-areas like the picture below. So far, I am only able to color the grid-lines. But I want to color the grid areas. What is the best way? Here is my code:
set terminal svg
set output 'out.svg'
set key off
set xlabel 'X'
set ylabel 'Y'
set title 'Data'
set grid
set grid xtics lw 0.25 lc rgb "#ff0000" # line only, but I want to color the whole area
#unset grid
#set grid ytics lt 0 lw 1 lc rgb "#0000ff"
set xrange [0:4]
set yrange [0:100]
set tics scale 0.5
set xtics nomirror
set ytics nomirror
set style fill solid noborder
set linetype 1 lc rgb 'red' lw 0.35
set linetype 2 lc rgb '#009900'
set linetype 3 lc rgb 'black' lw 0.5
set boxwidth 0.5 relative
set style fill solid border lc rgb "black"
plot "data.txt" using 1:2:4:3:5:($5 < $2 ? 1 : 2) linecolor variable with candlesticks, \
"data.txt" using 1:6 with lines lt 3, \
"data.txt" using 1:5:7 with filledcurves fs transparent solid 0.3 lc rgb "blue"
And here is my sample data.txt file for the plotting:
1 10 30 5 20 23 29
2 25 45 10 30 34 37
3 30 50 20 25 47 53
You could use a dummy function like [x=0:16:1] '+' us (x/2):(100/(int(x)%4!=1)) with filledcurves x1.
Every fourth point generates a NaN and interrupts the curve.
$data <<EOD
1 10 30 5 20 23 29
2 25 45 10 30 34 37
3 30 50 20 25 47 53
5 10 30 5 20 23 29
7 25 45 10 30 34 37
8 30 50 20 25 47 53
EOD
set style fill solid noborder
set linetype 1 lc rgb 'red' lw 0.35
set linetype 2 lc rgb '#009900'
set linetype 3 lc rgb 'black' lw 0.5
set boxwidth 0.5 relative
set style fill solid border lc rgb "black"
plot sample [x=0:16:1] '+' us (x/2):(100/(int(x)%4!=1)) with filledcurves x1 fc rgb "#EEEEEE",\
$data using 1:2:4:3:5:($5 < $2 ? 1 : 2) linecolor variable with candlesticks, \
$data using 1:6 with lines lt 3, \
$data using 1:5:7 with filledcurves fs transparent solid 0.3 lc rgb "blue"
This function could be also assigned to the y2 axis with fixed y2range, which might be more handy for interactive plots with zooming.

GNUPLOT : how to stop "histeps" from terminating to zero (y=0) at start and endpoints?

I am using the following script for plotting data points from file "delete.dat"
set terminal jpeg
set output "delete_histeps.jpeg"
set title "with histeps"
plot "delete.dat" using 1:2 index 0 pt 7 ps 0.2 lc "black" notitle,\
"delete.dat" using 1:2 index 1 pt 7 ps 0.2 lc "red" notitle,\
"delete.dat" using 1:2 index 2 pt 7 ps 0.2 lc "green" notitle,\
"delete.dat" using 1:2 index 0 with histeps lc "black",\
"delete.dat" using 1:2 index 1 with histeps lc "red",\
"delete.dat" using 1:2 index 2 with histeps lc "green"
and the "delete.dat" file is
1 2
3 4
5 6
7 8
9 10
-1 5
-2 3
-3 4
-4 2
-5 6
5 1
6 2
7 3
8 4
9 5
10 4
11 3
12 12
But the start and end step is dropping to zero, which I don't want. Though for step and fsteps command, the steps begin (end) at the first (last) data point, without dropping down to zero (y=0). Can you please suggest that works like histeps (which keeps data points in the middle of the steps) but does not terminate to zero (like steps or fsteps). I'm also attaching pictures.
If your data points are equidistant (like the ones you provided), an alternative hack would be possible as well by combining fsteps and steps and shifting the x-value by half the point-to-point-distance:
plot "delete.dat" i 0 pt 7 ps 2 lc "black" not, \
'' i 1 pt 7 ps 2 lc "red" not, \
'' i 2 pt 7 ps 2 lc "green" not, \
'' u ($1-1.0):2 i 0 w steps lc "black", '' u ($1+1.0):2 i 0 w fsteps lc "black" not, \
'' u ($1+0.5):2 i 1 w steps lc "red", '' u ($1-0.5):2 i 1 w fsteps lc "red" not, \
'' u ($1-0.5):2 i 2 w steps lc "green", '' u ($1+0.5):2 i 2 w fsteps lc "green" not
The trick here is that steps or fsteps alone would miss either the last or the first point and the line would end in mid-air, therefore both have to be plotted on top of each other with the same plotting style.
One must take care of the correct sign: if x is positive, steps get a downshift and fsteps get an upshift; and vice-versa if x is negative.
My answer is definitely not as "robust" as the one of theozh, but maybe easier to understand.
Well, that's how hispteps, steps, and fsteps have been defined.
Then, you have to create your own steps. A solution could be the following:
Code: (improved version)
### steps like histeps but not dropping to zero
reset session
set colorsequence classic
$Data <<EOD
1 2
3 4
5 6
7 8
9 10
-1 5
-2 3
-3 4
-4 2
-5 6
5 1
6 2
7 3
8 4
9 5
10 4
11 3
12 12
EOD
set table $myStep
do for [i=0:2] {
plot $Data u ($0==0?(x1=x2=$1):(x1=x2,x2=$1),(c=$0,x1+x2)/2.):2 index i w table
plot $Data u 1:2 every ::c index i w table # add last value
plot '+' u ("") every ::::1 w table # add two empty lines
}
unset table
set key top left
plot for [i=0:2] $myStep u 1:2 index i w step lc i lw 3 notitle,\
for [i=0:2] $Data u 1:2 index i w p pt 7 lc i notitle, \
### end of code
Result:

gnuplot - conditional colors in a points plot

I have the following data:
ClockIndex Max AvgStd Avg Num Threshold
"ck1 (1.54 GHz)" 35 +16.30 11 11583 X
"ck2 (1.54 GHz)" 28 +16.66 12 10669 -
"ck3 (1.54 GHz)" 29 +14.47 9 8036 -
"ck4 (1.54 GHz)" 35 +18.99 12 5685 -
"ck5 (1.54 GHz)" 9 +6.04 3 11 -
I'm plotting columns 2, 3, 4 together in a points plot, one line per entry.
Using this code:
set xtics rotate
set xlabel ""
set ylabel "Levels"
set title "Levels - foo"
set key autotitle columnhead
set term png medium size 1200,600
set grid
set output "foo.png"
plot "foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black
**Goal : If there's a "X" in the "Threshold" column, I want the "Max" point to be green, not blue.
I've tried using awk, but to no avail.
plot "< awk '{if($6 == \"X\") print }' foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black"
"< awk '{if($6 == \"-\") print }' foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "green",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black"
I keep getting errors like:
"foo_gnuplot", line 11: warning: Skipping data file with no valid points
Any help would be greatly appreciated! Thanks much for taking a look! :)
You can do it like this:
plot "foo.rpt" using 2:xticlabels(1) with points pt 13 ps 2 lt rgb "blue",\
"" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black",\
"" using (strcol(6) eq "X" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "green"
(condition)?($plotThisValue):(1/0) is a common gnuplot technique for conditional plotting.
With that code the original point is overprinted with the green one. (If you want to remove the original (now hidden) blue point completely you can use the same technique in line one of your plot command.)
Thanks much to havogt for the great answer! Here's what I ended up doing:
plot "foo.rpt" using 3:xticlabels(1) with points pt 13 ps 2 lt rgb "red",\
"" using 4:xticlabels(1) with points pt 13 ps 2 lt rgb "black",\
"" using (strcol(6) eq "X" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "green"
"" using (strcol(6) eq "-" )?($2):(1/0):xticlabels(1) with points pt 13 ps 2 lt rgb "blue"
Works great! Really appreciate the help!

how to define X's label on gnuplot

This is My data :
18_AGT_s 8234.00 8234.00 8234.00
18_MAC_s 8414.36 8308.36 8246.33
9_MAC_r 8414.36 8308.36 8246.33
9_MAC_s 8414.55 8309.55 8246.45
8_MAC_r 8414.55 8309.55 8246.45
8_MAC_s 8414.56 8310.08 8246.47
6_MAC_r 8414.56 8310.08 8246.47
6_MAC_s 8416.19 8310.21 8246.49
1_MAC_r 8416.19 8310.21 8246.49
and here is my gnuplot code :
plot "dat" using ($0+1):2 with linespoints pt 8 ps 2 lt 2 lw 4 lc rgb
"green" title "DMSR","dat" using ($0+1):3 with linespoints pt 5 ps 2
lt 3 lw 4 lc rgb "blue" title "Alarm","dat" using ($0+1):4 with
linespoints pt 6 ps 2 lt 4 lw 4 lc rgb "red" title "Emergency"
and here is my out put :
But In the step part I want to have 18_AGT_s and 18_MAC_s and 9_MAC_s and .... for example on the X part I want to have 18_AGT_s in stand of 1 or I want to have 18_MAC_2 instand of 2 and 9_MAC_r instand of 3 etc. Any help thanks
Use the xticlabels() option with the column number with the labels as argument (1, in this case):
# Optionally rotate labels so they fit
set xtics rotate
plot "dat" using ($0+1):2 with linespoints pt 8 ps 2 lt 2 lw 4 lc rgb \
"green" title "DMSR","dat" using ($0+1):3 with linespoints pt 5 ps 2 \
lt 3 lw 4 lc rgb "blue" title "Alarm","dat" using \
($0+1):4:xticlabels(1) with linespoints pt 6 ps 2 lt 4 lw 4 lc rgb \
"red" title "Emergency"
Here you only need to use it for the last plot instance so that it overwrites the number options.

Resources