2

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution :) Would be helpful if you could add the gnuplot version you used.
@Gilfoyle this was created with gnuplot 5.2.8 but it works for gnuplot>=5.0.0. If you have gnuplot 4.x you need some adaptions (i.e. read data from file, because no datablocks at that time).
1

I actually found a solution using Python and Matplotlib.

import numpy as np
import matplotlib.pyplot as plt
import random
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')


DIM = 3;

# Unit cube
cube = [[[0.0,1.0],[0.0,0.0],[0.0,0.0]],\
        [[0.0,0.0],[0.0,1.0],[0.0,0.0]],\
        [[0.0,0.0],[0.0,0.0],[0.0,1.0]],\
        [[1.0,1.0],[0.0,1.0],[0.0,0.0]],\
        [[1.0,0.0],[1.0,1.0],[0.0,0.0]],\
        [[1.0,1.0],[0.0,0.0],[0.0,1.0]],\
        [[1.0,1.0],[1.0,1.0],[0.0,1.0]],\
        [[0.0,0.0],[1.0,1.0],[0.0,1.0]],\
        [[0.0,0.0],[0.0,1.0],[1.0,1.0]],\
        [[0.0,1.0],[0.0,0.0],[1.0,1.0]],\
        [[1.0,1.0],[0.0,1.0],[1.0,1.0]],\
        [[0.0,1.0],[1.0,1.0],[1.0,1.0]]]

# Number of Cubes
numb_Cubes = 5

# Array with positions [x, y, z]
pos = [[0 for x in range(DIM)] for y in range(numb_Cubes)]   
for k  in range(numb_Cubes):
    for d in range(DIM):
        pos[k][d] = random.uniform(-1,1)

# Size of cubes
size_of_cubes = [0 for y in range(numb_Cubes)]  
for k in range(numb_Cubes):
    size_of_cubes[k] = random.random()

# Limits
xmin, xmax = -1, 1
ymin, ymax = -1, 1
zmin, zmax = -1, 1

for n in range(numb_Cubes):
    for k in range(len(cube)):
            x = np.linspace(cube[k][0][0]*size_of_cubes[n]+pos[n][0], cube[k][0][1]*size_of_cubes[n]+pos[n][0], 2)
            y = np.linspace(cube[k][1][0]*size_of_cubes[n]+pos[n][1], cube[k][1][1]*size_of_cubes[n]+pos[n][1], 2)
            z = np.linspace(cube[k][2][0]*size_of_cubes[n]+pos[n][2], cube[k][2][1]*size_of_cubes[n]+pos[n][2], 2)

            ax.plot(x, y, z, 'black', lw=1)
            ax.set_xlim([xmin,xmax])
            ax.set_ylim([ymin,ymax])
            ax.set_zlim([zmin,ymax])

The result I get:

enter image description here

I am still interested in a solution for gnuplot or a faster solution for Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.