Search code examples
plotgnuplotclip

gnuplot - Properly clipping a 3D plot (splot)


Disclaimer: I am totally new to Gnuplot, so I am probably missing something obvious...

For a presentation that I have to give, I am trying to use Gnuplot for creating an interactive version of this plot.

Up to now, I have come up with the following code:

set hidden3d
set isosamples 40
set border 4095
set xrange [-1 : 5]
set yrange [-3 : 3]
set zrange [-10 : 10]
set xtics 2
set ytics 2
set ztics 2
f(x,y) = x**2 + y**2 * (1 - x)**3
splot f(x,y)
pause -1

However, at the boundary of the plot (specifically, at the boundary with respect to the z-axis) the plot gets truncated in a way that I find quite ugly; see below. What I would like is essentially that all the lines get drawn as if my plot range would be (e.g.) -50 : 50, but then in the end only the "intersection" of the plot with the box [-1:5] x [-3:3] x [-10:10] should actually be shown. What seems to be happening right now is that any "plot segment" that has at least one point outside the box determined by xrange-zrange gets not drawn at all.

(How) can I change this?

Resulting plot, showing the ugly clipping


Solution

  • Answering from the future, sort of, so this may not count...

    Versions of gnuplot through 5.2 cannot do exactly what you want. However the development version 5.3 does this by default so long as you use a pm3d surface rather than a hidden3d surface. Here is your plot as rendered by the current development code:

    set border 4095
    unset colorbox
    set view 56, 15, .75, 1.75
    set samples 40, 40
    set isosamples 40, 40
    set xyplane 0
    set grid x y z vertical
    set pm3d depthorder border linewidth 0.100
    set pm3d clip z 
    set pm3d lighting primary 0.8 specular 0.3 spec2 0.3
    
    set xrange [-1 : 5]
    set yrange [-3 : 3]
    set zrange [-10 : 10]
    set xtics 1 offset 0,-0.5
    set ytics 1 offset 0,-0.5
    set ztics 5
    
    f(x,y) = x**2 + y**2 * (1 - x)**3
    splot f(x,y) with pm3d fillcolor "cyan"
    

    The key is the command set pm3d clip z. This will be the default in future versions but is not supported in version 5.2. You can build from the current git source repository on gnuplot.sf.net or wait for the release of version 5.4 expected in Spring 2020. enter image description here