Search code examples
colorsgnuplotgradient

Gnuplot 5: color gradient shading between curves


This was created with Matplotlib. Is it possible to make the same type of shading in Gnuplot 5?

color gradient shading


Solution

  • I'm not aware that gnuplot has a gradient fill option, but I could be wrong. The following is a bit of an "ugly" workaround. You basically create 3 plots on top of each other. You might want to adjust the palette to get the desired colors and a smooth transition.

    1. a dummy plot to get the palette as background (i.e. the colorbox as large as the graph)
    2. cover the part above y>f(x) and y>0 to x2-axis as well as below y<f(x) and y<0 to x1-axis.
    3. plot again f(x) to see f(x) and the axes tics again

    Edit: The earlier version of the code used multiplot. It's not necessary, just use set colorbox back. But then set xzeroaxis ls -1 is not visible anymore, add plot 0 w l ls -1 instead.

    Code:

    ### filled curve with gradient
    reset session
    
    f(x) = sin(x)/(1+x)
    fabove(x) = f(x)<0 ? 0 : f(x)
    fbelow(x) = f(x)>0 ? 0 : f(x)
    
    set samples 200
    set palette defined (0 "white", 1 "red", 2 "black")
    
    set colorbox back user origin graph 0, graph 0 size graph 1, graph 1
    unset cbtics
    
    set xrange[0:15]
    set xzeroaxis ls -1
    set yrange[-0.2:0.5]
    
    plot fabove(x) w filledcurves x2 fc rgb "white" not, \
         fbelow(x) w filledcurves x1 fc rgb "white" not, \
         f(x) w l lw 2 lc rgb "black", \
         NaN palette, \
         0 w l ls -1
    ### end of code
    

    Result:

    enter image description here