Plotly Volume组件无法渲染随机分布3D顶点问题咨询及数据适配疑问
First off, let's get to the core issue: Plotly's Volume trace doesn't work with unstructured scatter points—even if you sort them. This component is designed for regular 3D Cartesian grids (structured data where each dimension has a fixed number of evenly spaced points, forming orthogonal grid lines). Randomly generated points lack this grid structure, so Plotly can't interpret them as a continuous volume to render isosurfaces.
What's Wrong with Your Code?
When you use np.random.uniform to generate X/Y/Z, you're creating unstructured scatter data. Sorting these points doesn't fix the problem because they still don't form a regular grid. The official example relies on np.mgrid precisely because it generates this structured grid (each axis has a fixed step size and count, so every combination of X/Y/Z points forms a predictable cube-like structure).
How to Fix It
Option 1: Use Regular Grid Data (Works Directly)
Stick with np.mgrid to generate your coordinates, just like the official example. Here's how to adapt your logic:
import numpy as np import plotly.graph_objects as go # Generate a regular 3D grid with fixed steps X, Y, Z = np.mgrid[0:1:100j, 0:1:100j, 0:1:100j] # Calculate values across the grid V = np.sin(X) * np.sin(Y) + Z fig = go.Figure(data=go.Volume( x=X.flatten(), y=Y.flatten(), z=Z.flatten(), value=V.flatten(), isomin=np.min(V), isomax=np.max(V), opacity=0.1, surface_count=20, colorscale='Spectral', reversescale=True )) fig.show()
Option 2: Interpolate Scatter Data to a Regular Grid
If your third-party data is unstructured scatter points (shape (N,3) with corresponding values), you need to interpolate them onto a regular grid first. Use scipy.interpolate.griddata for this:
import numpy as np import plotly.graph_objects as go from scipy.interpolate import griddata # Your unstructured scatter data (example) np.random.seed(42) coords = np.random.uniform(0, 1, (30000, 3)) X_scatter, Y_scatter, Z_scatter = coords[:,0], coords[:,1], coords[:,2] V_scatter = np.sin(X_scatter) * np.sin(Y_scatter) + Z_scatter # Create a regular grid to interpolate onto xi, yi, zi = np.mgrid[0:1:50j, 0:1:50j, 0:1:50j] # Interpolate scatter values onto the grid V_interp = griddata((X_scatter, Y_scatter, Z_scatter), V_scatter, (xi, yi, zi), method='linear') # Plot the interpolated volume fig = go.Figure(data=go.Volume( x=xi.flatten(), y=yi.flatten(), z=zi.flatten(), value=V_interp.flatten(), isomin=np.nanmin(V_interp), isomax=np.nanmax(V_interp), opacity=0.1, surface_count=20, colorscale='Spectral', reversescale=True )) fig.show()
Note: You might get NaN values in areas with no scatter points—you can handle these by filtering or switching to the nearest interpolation method.
Where to Host Your NumPy Dataset?
For sharing NumPy .npy datasets:
- GitHub Gist: Quick, free, and great for small to medium datasets. You can upload
.npyfiles directly or include code to regenerate the data. - Kaggle Datasets: Ideal for larger datasets or data science-related projects; easy to share and discover.
- Zenodo: Perfect for academic datasets, as it provides DOIs for formal citation.
- Stack Overflow Question: For small datasets, convert the array to a CSV (using
np.savetxt('data.csv', coords, delimiter=',')) and paste the content in a code block.
内容的提问来源于stack exchange,提问作者Simon T.

