Search code examples
gnuplotfillx-axis

Fill area below curve and x-axis based on x-value in gnuplot


I want to fill the area between the curve and the x-axis from xrange 1950 (start of data) to 2020. I tried doing this with

plot [..] with filledcurves below x1=2020

but this produces a plot where the y-scale is messed up and looks just wrong.

I've tried to illustrate what I want with the attached figure (original to the left, what I want to the right).

I appreciate any tips!

enter image description here


Solution

  • You don't write whether you have a function or a datafile. Well, I was struggeling with limiting the range of the filledcurve. Something like the following (which I would consider straightforward) doesn't work: (I still don't fully understand why). It gives a message warning: Ignoring sample range in non-sampled data plot and not the expected result (gnuplot 5.2.8).

    plot [1950:2020] $Data u 1:2 w filledcurves x1 lc "red", \
         [1940:2100] '' u 1:2 w l lw 2 lc "black"
    

    So, instead I used the ternary operator to limit the filled xrange.

    Script:

    ### fill below a part of a curve
    reset session
    
    # create some test data
    f(x) = 2.5e5*(x-1900)**2
    set table $Data 
        plot sample [1940:2100:5] '+' u 1:(f($1)) w table
    unset table
    
    unset key
    set grid xtics, ytics front
    set xrange [1940:2100]
    set style fill solid 0.3
    
    LimitRange(x,x0,x1) = x0<=x && x<=x1 ? x : NaN
    
    plot $Data u (LimitRange($1,1950,2020)):2 w filledcurves x1 lc "red", \
            '' u 1:2 w l lw 2 lc "black"
    ### end of script
    

    Result:

    enter image description here