Search code examples
gnuplot

How to show a grid with two filled plots


I am plotting to datasets with 'fillsteps' one below another and I want the plot two show only the area that is a difference between the two

plot [0:1][0:1] x with fillsteps above fill solid not,x**2 with fillsteps above fill solid lc rgb 'black' not

But the grid obviously gets blocked in this case: enter image description here

Is there any way to create something like a cross-section between the two areas, show the grid and get rid of those nasty artifacts that are seen below?


Solution

  • Ok, basically you want to fill the area between two curves (either lines or steps) and have a grid on top. The set grid front you found yourself, but let me make another suggestion.

    • For the first case (lines), i.e. a fill between lines, you can simply use 3 columns (check help filledcurves) and then the area will be filled between the curves:
    plot '+' u 1:(f1(x)):(f2(x)) w filledcurves
    
    • For the second case (steps), I don't see (yet) such an option with filledsteps. Actually, from your option above I assume you are using gnuplot5.5.

    In general, I wouldn't call it a "clean" solution if you plot something and partly have to cover it with something else with background color. What if you want a transparent background? A transparent color which covers something colored has not yet been invented ;-), there is no such "invisible" color. For another example check this. Furthermore, with fillsteps I can also observe the artifacts of vertical gap lines which you see in your graph, but I don't have a good solution to avoid them.

    Hence, my suggestion is to plot only there where you need something to plot. Actually, you can mimic the fillsteps behaviour. It's not obvious how to do it, but not too difficult. While you plot line by line, you remember the previous x-value and function values of f1(x0) and f2(x0) in x0, y0 and y2, respectively. You plot with the plotting style boxxyerror (check help boxxyerror) using x:y:xlow:xhigh:ylow:yhigh.

    Script: (works with at least gnuplot>=5.0.0)

    ### plotting style filledcurves and mimic fillsteps "between"
    reset session
    
    f1(x) = x
    f2(x) = x**2
    
    set xrange[0:1]
    set yrange[0:1]
    set key noautotitle
    set grid x,y front lw 1.3
    set style fill solid 1.0 border
    set samples 50
    
    set multiplot layout 2,1
    
        plot '+' u 1:(f1(x)):(f2(x)) w filledcurves
    
        plot x1=y1=y3=NaN '+' u (x0=x1,x1=$1):(y0=y1,y1=f1($1),y2=y3,y3=f2($1)):(x0):(x1):\
                             (y0):(y2) w boxxy
    
    unset multiplot
    ### end of script
    

    Result: (download the PNG image and check that the background is transparent).

    enter image description here