Search code examples
labelgnuplot

How to hide a label behind a 3D object in Gnuplot?


The following gnuplot code produces a short animation of a sphere embedded within a tetrahedron with vertices labled "1" through "4". As vertex "2" of the tetrahedron rotates behind the sphere, it remains visible. When label "2" is behind the sphere, how can it be hidden?

set terminal aqua size 500 500 font "Helvetica,20"
 set style circle; set style fill solid
 set xrange [-1:1]; set yrange [-1:1]; set zrange [-1:1]
 set size ratio -1
 set parametric; set isosamples 31,61; set hidden3d
 set urange [-pi/2:pi/2]; set vrange [0:2*pi]
 unset border; unset xtics; unset ytics; unset ztics
 rot_x=90; rot_z=90; scale=1.5; scale_z=1
 set view rot_x, rot_z, scale, scale_z
 set label 1 "1" at graph .6543,.8492,.1324 center back
 set label 2 "2" at graph .1922,.1316,.2755 center back
 set label 3 "3" at graph .2273,.7511,.8788 center back
 set label 4 "4" at graph .9262,.2681,.7133 center back
 do for [i=90:180] {set view 75, i; r=0.577;\
 splot r*cos(u)*cos(v),r*cos(u)*sin(v),r*sin(u) t "" w l lw 0.5 lc rgb "blue",\
 "r1.dat"  u 1:2:3:(0.01)  t "" with circles,\
  "" u 1:2:3  t "" w l lc rgb "red"}

The datafile "r1.dat" is:

.2911 .6589 -.6936
-.5808 -.6952 -.4236


.2911 .6589 -.6936
-.5144 .4738 .7147


.2911 .6589 -.6936
.8041 -.4375 .4025


-.5808 -.6952 -.4236
-.5144 .4738 .7147


-.5808 -.6952 -.4236
.8041 -.4375 .4025


-.5144 .4738 .7147
.8041 -.4375 .4025

Solution

  • It is possible to let your label disappear when you are using the plotting style with labels. You need to find a reasonable offset for the labels. Here, I added only a z-offset for the label depending whether z>0 and z<0.

    I was not sure what you want to plot with circles, so I replaced it with points. Furthermore, I shortened your tetrahedron data a bit, however, at the cost of a longer plot command, but if you need to change coordinates you only have to do it only at one place.

    There is certainly room for fine tuning.

    Script:

    ### make labels disappear behind hidden3d
    reset session
    
    $Tetrahedron <<EOD
     0.2911   0.6589   -0.6936   1
    -0.5808  -0.6952   -0.4236   2
    -0.5144   0.4738    0.7147   3
     0.8041  -0.4375    0.4025   4
    EOD
    
    set xrange [-1:1]
    set yrange [-1:1]
    set zrange [-1:1]
    set size ratio -1
    set view equal xyz
    set parametric
    set isosamples 31,61
    set hidden3d
    set urange [-pi/2:pi/2]
    set vrange [0:2*pi]
    unset border
    unset tics
    set view rot_x=90, rot_z=90, scale=1.5, scale_z=1
    set key noautotitle
    r=0.577
    
    PosZ(colZ) = sgn(column(colZ))*0.07 + column(colZ)
    
    do for [i=0:358:2] {
        set view 75, i
        splot r*cos(u)*cos(v),r*cos(u)*sin(v),r*sin(u) w l lw 0.5 lc "blue",\
        for [i=1:3] $Tetrahedron u 1:2:3 every i w lp pt 7 lc "red", \
                  '' u 1:2:3 every 2::1 w lp pt 7 lc "red", \
                  '' u 1:2:(PosZ(3)):4 w labels
    }
    ### end of script
    

    Result: (screen capture from wxt terminal)

    enter image description here