Search code examples
gnuplot

Fill the area between two curves


I have two functions and want to fill the area between them

enset xlabel "x"
set ylabel "y"

set yrange[-20:30]
set xrange[-30:120]

plot 10*sin(0.2*x) title "f(x) = 10 sin(0.2x)" lw 2
replot sqrt(x) title "g(x) = sqrt(x)" lw 2
replot [0:73.4] -sqrt(x)+10*sin(0.2*x) with boxes title "Área de intersección"

But I didn't get what i expected. How can I fill this space correctly?


Solution

  • f(x) = 10*sin(0.2*x)
    g(x) = sqrt(x)
    
    set key opaque box
    
    set xrange [0:73.4]
    
    plot '+' using 1:(f(x)):(g(x)) with filledcurves between fillcolor "grey" notitle, \
         f(x) with lines lt 2 lw 2 title "f(x)", \
         g(x) with lines lt 3 lw 2 title "g(x)"
    

    Adjust titles, colors, linewidth as you like.

    enter image description here