Search code examples
3dlabelgnuplot

How to rotate tic labels in 3D in gnuplot?


Rotation of tic labels in 3D might be necessary, especially if you have many text tic labels which might overlap if not rotated or in order to get a better separation between the tic labels. Rotation in 2D is no problem.

Script: (requires gnuplot 5.4.0 because of set boxdepth)

### how to rotate xticlabels in 3D?
reset session

$Data <<EOD
 Alpha   One     1.00
 Alpha   Two     2.00
 Alpha   Three   3.00
 Alpha   Four    4.00
 Beta    One     1.25
 Beta    Two     2.25
 Beta    Three   3.25
 Beta    Four    4.25
 Gamma   One     1.50
 Gamma   Two     2.50
 Gamma   Three   3.50
 Gamma   Four    4.50
 Delta   One     1.75
 Delta   Two     2.75
 Delta   Three   3.75
 Delta   Four    4.75
EOD

set xrange [-0.5:3.5]
set yrange [-0.5:3.5]
set grid x,y
set key noautotitle

set multiplot layout 1,2

    set size square
    set xlabel "x axis label"
    set xtic rotate by 45 right
    set ylabel "y axis label"
    set ytic rotate by 45 right
    plot $Data u (int($0)/4):(int($0)%4):3:xtic(1):ytic(2) w p pt 5 ps 5 lc palette

    set view 60,30
    set xlabel rotate parallel offset 0,0,0
    set xtic rotate by 45 right    # apparently doesn't have any effect in 3D
    set ylabel rotate parallel offset 0,0,0
    set ytic rotate by -45 left    # apparently doesn't have any effect in 3D
    set xyplane at 0
    set boxwidth 0.5
    set boxdepth 0.5
    splot $Data u (int($0)/4):(int($0)%4):3:xtic(1):ytic(2) w boxes lc palette z

unset multiplot
### end of script

Result: (wxt terminal)

enter image description here

However, it looks like tic labels cannot be rotated in 3D, only in 2D. It's not explicitly mentioned in help tics that it cannot be done in 3D.

rotate asks gnuplot to rotate the text through 90 degrees, which will be done if the terminal driver in use supports text rotation. norotate cancels this. rotate by asks for rotation by degrees, supported by some terminal types.

Apparently, wxt terminal supports text rotation, but not in 3D.

  • There was a question about rotation of axis labels which apparently can be done by rotate parallel, but not at arbitrary angles and not for the tic labels.

  • There was also a question about projecting axis labels on to the xy-plane, which apparently can only be done without an awkward workaround.

Question: How to rotate numeric or text tic labels 3D-plots with splot?


Solution

  • Here is a tedious workaround, basically by setting the rotated tic labels manually.

    Comments:

    • the view is set to: set view 45,30, but I haven't found out yet by how much I have to rotate the tics. Instead, I have to fiddle around to find the proper looking angles (i.e. inline with the grid lines). Maybe I could do this with some geometric formula depending on the tilt and viewing angle.

    • although you can rotate the axis labels at least by rotate parallel, at least the ylabel doesn't look like it's aligned parallel to the y-axis. At least, it seems to be ok for angles 0° and 90°.

    • instead of using label offsets, it seems to be easier to add some spaces ' ' in front or in the back to shift the label away from the axes.

    If anyone knows a better solution, please let me know.

    Script:

    ### workaround for rotating tic labels in 3D
    reset session
    
    $Data <<EOD
     Alpha   One     1.00
     Alpha   Two     2.00
     Alpha   Three   3.00
     Alpha   Four    4.00
     Beta    One     1.25
     Beta    Two     2.25
     Beta    Three   3.25
     Beta    Four    4.25
     Gamma   One     1.50
     Gamma   Two     2.50
     Gamma   Three   3.50
     Gamma   Four    4.50
     Delta   One     1.75
     Delta   Two     2.75
     Delta   Three   3.75
     Delta   Four    4.75
    EOD
    
    set xrange [-0.5:3.5]
    set yrange [-0.5:3.5]
    set grid x,y
    set key noautotitle
    
    set view 45,30
    set xyplane at 0
    set xlabel "x axis label" rotate parallel offset 0,-1.2,0
    set ylabel "y axis label" rotate parallel
    set xtics ()
    set format x ""
    set ytics ()
    set format y ""
    
    myXLabel(i) = word($Data[i],1) 
    myYLabel(i) = word($Data[i],2) 
    set boxwidth 0.5
    set boxdepth 0.5
    
    set for [i=1:4] label 100+i at first i-1,graph 0,   graph 0     myXLabel(i*4).' '   rotate by  40 right offset 0,0,0
    set for [i=1:4] label 200+i at graph 1,  first i-1, graph 0 ' '.myYLabel((i-1)%4+1) rotate by -15 left  offset 0,0,0
    
    splot $Data u (int($0)/4):(int($0)%4):3 w boxes lc palette z
    ###  end of script
    

    Result:

    enter image description here