Search code examples
gnuplotvisualization

Plot transparent 3D boxes using gnuplot


I have a data file "data.txt" which contains the coordinates of the borders of several boxes in three dimensions. Each line represents a single box. The file contains over 100 boxes.

        x_Min x_Max y_Min y_Max z_Min z_Max
        -0.2 0.2 -0.2 0.2 -0.2 0.2
        0.2 0.4 -0.2 0.2 -0.2 0.2
        ....
        ...
        ..

Now I want to plot that. In two dimensions it is very easy by using

plot "boxes.txt" u 1:2:3:4 w boxxyerrorbars 

With (x-Value):(y-Value):(Half Width):(Half Height).

Than I get this: enter image description here

But how can I achieve this in three dimensions? I didn't find any solution for this problem.


Solution

  • In case you are still interested in a gnuplot solution...

    If it is sufficient to just draw the edges of the boxes you can use the plotting style with vectors. You simply need to select the necessary columns and plot all edges in 3 loops. Here gnuplot's integer division (e.g. 1/2=0) is helpful.

    However, if you want to plot surfaces and hide surfaces if they are covered by another box you'd better use with pm3d (check help pm3d). Then, however, you have to re-shape your input data.

    Script:

    ### plot edges of boxes in 3D
    reset session
    
    $Data <<EOD
    x_Min  x_Max  y_Min  y_Max  z_Min  z_Max
     -0.2   0.2   -0.2    0.2   -0.2    0.2
      0.3   0.4   -0.1    0.2   -0.1    0.2
     -1.5  -0.5   -1.2   -0.4   -0.9    0.0
      0.5   1.0   -1.0   -0.5   -0.5   -0.1
      0.0   0.3   -1.4   -1.1   -1.0   -0.7
    EOD
    
    set xyplane relative 0
    set view equal xyz
    set view 60,30,1.7
    set xtics 0.5
    set ytics 0.5
    set ztics 0.5
    set key noautotitle
    
    splot for [i=0:3] $Data u 1:i/2+3:i%2+5:($2-$1):(0):(0):0 w vec lc var nohead, \
          for [i=0:3]    '' u i/2+1:3:i%2+5:(0):($4-$3):(0):0 w vec lc var nohead, \
          for [i=0:3]    '' u i/2+1:i%2+3:5:(0):(0):($6-$5):0 w vec lc var nohead
    ### end of script
    

    Result:

    enter image description here