Plotly Volume not rendering random distribution of points

Question:

I have 3D vertices from a third-party data source. The plotly Volume object expects all the coordinates as 1D lists. The examples on their website use the mgrid function to populate the 3D space into the flatten function to get the 1D lists of each axis.
https://plotly.com/python/3d-volume-plots/

I don’t understand why my approach produces an empty plot.
coords is my list of vertices in the shape of (N, 3).

See the following code snippet that draws random coordinates, sorts them, but results in an empty render.

X = np.random.uniform(0, 1, 30000)
Y = np.random.uniform(0, 1, 30000)
Z = np.random.uniform(0, 1, 30000)
coords = np.dstack((X.flatten(), Y.flatten(), Z.flatten()))[0]

sort_idx = np.lexsort((coords[:, 0], coords[:, 1], coords[:, 2]))
coords = coords[sort_idx]

X=coords[:, 0]
Y=coords[:, 1]
Z=coords[:, 2]
V = np.sin(X) * np.sin(Y) + Z

fig = go.Figure(data=go.Volume(
    x=X,
    y=Y,
    z=Z,
    value=V,
    isomin=np.min(Z),
    isomax=np.max(Z),
    opacity=0.1, # needs to be small to see through all surfaces
    surface_count=20, # needs to be a large number for good volume rendering
    colorscale='Spectral',
    reversescale=True
    ))
fig.show()

Update: It seems like plotly expects the coordinates to be sorted.

X, Y, Z = np.mgrid[-50:50:40j, -50:50:40j, -8:8:10j]
coords = np.dstack((X.flatten(), Y.flatten(), Z.flatten()))[0]
np.random.shuffle(coords)

Shuffling the list like this and plugging coords into the code above produces an empty Volumn render.

I now tried to sort my data points, but I still get an empty render. How can I share my dataset? npfile, but where should I host it?

sort_idx = np.lexsort((coords[:, 0], coords[:, 1], coords[:, 2]))
coords = coords[sort_idx]

Update 2: Using a uniform random distribution to generate the coordinates results in a vertex list that seems to be not processable by plotly even after sorting.

X = np.random.uniform(0, 1, 30000)
Y = np.random.uniform(0, 1, 30000)
Z = np.random.uniform(0, 1, 30000)
coords = np.dstack((X.flatten(), Y.flatten(), Z.flatten()))[0]
Asked By: Simon T.

||

Answers:

I have not read the documentation closely enough.

Draws volume trace between iso-min and iso-max values with coordinates given by four 1-dimensional arrays containing the value, x, y and z of every vertex of a uniform or non-uniform 3-D grid.

The input has to be in the form of an orthogonal grid; thus a cloud of random points is not processable by Volumn. Non-uniformity means in this case that along one axis (e.g., z) the "layers" don’t have to be evenly spaced. Furtunatly my data is almost in a grid shape which is fixable by interpolation.

Answered By: Simon T.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.