Search code examples
gnuplot

Remove padding between two filledcurves in gnuplot


I am trying to plot two filledcurves side by side. But they are not touching each other, instead there is a white gap in between. How to remove it?

I tried to decrease the start and increase the end x value of the midde filledcurve but still there is a gap between them. It's like some default behavious of filledcurves.

Here's the gnuplot script

set title "Quantile"
set yrange[0:1.1]
set xlabel("x")

N(x) = 1/(sqrt(2*pi))*(exp(-x**2/2))
N1(x) = (x<=-1.96 || x>=1.96)? 1/(sqrt(2*pi))*(exp(-(x)**2/2)): 1/0
N2(x) = (x>=-1.96 && x<=1.96)? 1/(sqrt(2*pi))*(exp(-x**2/2)):1/0
set output "Quantile.png"
plot N1(x)  with filledcurves y1=0 lc "cyan", N2(x) with filledcurves y1=0, N(x) with lines title "Normal Distribution" lw 2.5,
pause -1

The output, I am getting

enter image description here


Solution

  • When plotting functions, gnuplot uses a certain number of samples, default is 100 (check help samples). So, increase this number until the gap is sufficiently small.

    So, add a line, for example:

    set samples 1000
    

    Addition:

    Here is an alternative approach using plotting style with boxes and variable color and of course set samples 1000. Check help boxes and help variable. Instead of plot f(x) you could also write plot '+' u 1:(f($1)), check help special-filenames.

    I'm not sure if there is a way using with filledcurves and variable color.

    Script:

    ### fill area under curve with variable color
    reset session
    
    set xlabel "x"
    set xrange[-5:5]
    set yrange[0:0.42]
    set key noautotitle
    set style fill solid 1.0 noborder
    set samples 1000
    
    N(x)       = 1/(sqrt(2*pi))*(exp(-x**2/2))
    myColor(x) = abs(x)<=1.96 ? 0xff0000 : 0x0000ff
    
    plot '+' u 1:(N($1)):(myColor($1)) w boxes lc rgb var, \
         N(x) w lines title "Normal Distribution" lw 2.5 lc "black"
    ### end of script
    

    Result:

    enter image description here