'Gnuplot : filledcurve between smoothed curve
I have two sets of points: "test1.dat" and "test2.dat", they do not share same X-values.
I want to draw two smooth lines first due to the data is noisy, and then draw a filledcurve between the smooth lines.
I've read the tutorial, and cannot find the answer.
Solution 1:[1]
Is far as I know Gnuplot cannot make plots using data in 2 different files. In such cases I invoke a BASH program such as "paste" to merge the two file. I assume the two files contain data in the format "X Y" and they share a common X-grid (the number of datapoints must also be equal)
plot '<paste test1.dat test2.dat' u 1:2:4 w filledcurve
PS: If you are not using Linux I don't knwo how to do it....
Solution 2:[2]
This works for me. First fill with some color between the upper smoothed line and x axis, second fill white between the lower line and x axis, finally plot the two smoothened lines.
smooth cspline must be before with filledcurve
plot "test1.dat" using ($1):($2) notitle smooth cspline with filledcurve x1 lt rgb "#FFAAAA",\
"test2.dat" using ($1):($2) notitle smooth cspline with filledcurve x1 lt rgb "#FFFFFF",\
"test1.dat" using ($1):($2) notitle w l lw 2 lt 1 lc 0 smooth cspline,\
"test2.dat" using ($1):($2) notitle w l lw 2 lt 1 lc 1 smooth cspline
Solution 3:[3]
As @mgilson already described in his solution: reverse one dataset and let gnuplot close and fill the curve.
So, just for fun and the records. I haven't found any way to do it without the help of external tools with the gnuplot version at the time of OP's question (gnuplot 4.6.0). However, starting at least from gnuplot 5.0.3 when plotting style with table was introduced, there is a way, and even a simpler version for gnuplot>=5.2.0.
I'm aware that this is probably less efficient than with external tools, but it is gnuplot-only and hence really platform-independent!
As the OP wants to plot the noisy curves but only wants to fill the smoothed curves, simply plot the smoothed curves first into a datablock or file (as @mgilson did in his answer).
Script: (for gnuplot>=5.0.3)
Explanation:
- the second dataset will be mirrored at the y-axis
(-$1):2and "resorted" viasmooth uniqueand written into a third dataset, i.e. in total it is reversed in x. - the first dataset and the (once more) mirrored third dataset will be appended into a fourth dataset which will be used for filling.
### filledcurves for 2 datasets with different x-values
# reguires gnuplot>=5.0.3
reset session
$Data1 <<EOD
-5 5
-2 4
0 1
1 2
3 5
5 4
7 2
9 4
EOD
$Data2 <<EOD
-5 7
-1.0 6
0.5 4
1.5 6
2.5 7
6.5 5
8.0 4
9 5
EOD
set table $Data3
plot $Data2 u (-$1):2 smooth unique
set table $Data4
plot $Data1 u 1:2 w table, \
$Data3 u (-$1):2 w table
unset table
set style fill solid 0.2 noborder
plot $Data4 u 1:2 w filledcurves lc "red" ti "filledcurves", \
$Data1 u 1:2 w lp pt 7 ti "Data1", \
$Data2 u 1:2 w lp pt 7 ti "Data2"
### end of script
Script: (for gnuplot>5.2.0)
Explanation:
- this is feasible only since gnuplot 5.2.0 when indexing of datablocks was introduced
- data needs to be in a datablock (see here: gnuplot: load datafile 1:1 into datablock)
- the first dataset will be written line by line into a new dataset
- the second dataset will be appended in reverse order
### filledcurves for 2 datasets with different x-values
# reguires gnuplot>=5.2.0
reset session
$Data1 <<EOD
-5 5
-2 4
0 1
1 2
3 5
5 4
7 2
9 4
EOD
$Data2 <<EOD
-5 7
-1.0 6
0.5 4
1.5 6
2.5 7
6.5 5
8.0 4
9 5
EOD
set print $Data3
do for [i=1:|$Data1|] { print $Data1[i] }
do for [i=|$Data2|:1:-1] { print $Data2[i] }
set print
set style fill solid 0.2 noborder
plot $Data3 u 1:2 w filledcurves lc "red" ti "filledcurves", \
$Data1 u 1:2 w lp pt 7 ti "Data1", \
$Data2 u 1:2 w lp pt 7 ti "Data2"
### end of script
Result: (very similar for both scripts)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Raphael Roth |
| Solution 2 | Petr |
| Solution 3 | theozh |

