Search code examples
gnuplotdensity-plot

How to make a density plot using gnuplot


For a piecewise function

f(x,y) = x < 0 ? 0 : 0 <= x && x < 1 && x*y < 1 ? x : 1

which breaks down to following in Mathematica language

f(x,y) = 0 if x < 0
f(x,y) = x if 0 <= x < 1 & xy < 1
f(x,y) = 1 otherwise

I want to make the density plot, which should look like the following figure

enter image description here

How to go about it? I tried doing it like this

set hidden3d
set dgrid3d 50,50 qnorm 2
set view map
splot f(x,y) 

which I know is incorrect.

Edit: Earlier, by mistake I wrote xy < 1 instead of x*y < 1 when defining piecewise function. I have corrected that.


Solution

  • In your f(x,y), xy should read x*y. Check the following. Play with the sampling, i.e. set samples and set isosamples to get higher or lower resolution. Furthermore, check help pm3d and help set palette.

    Code:

    ### plot piecewise function as a map
    reset session
    
    set size ratio 0.5
    f(x,y) = x < 0 ? 0 : 0 <= x && x < 1 && x*y < 1 ? x : 1
    
    set samples 400
    set isosamples 200
    set palette defined (0 '#26548a', 0.5 '#e59e4a', 1 '#fff2bf')
    set view map
    
    set xlabel "d"
    set xrange [-2:2]
    set xtics 1
    set ylabel "b"
    set yrange [0:2]
    
    splot f(x,y) w pm3d
    ### end of code
    

    Result:

    enter image description here