I'm struggling with making Python Plotly to show a 3D volume plot (isosurface contours) in Spyder or Jupyter Notebook.
I am successful with a simple example copied from the web:
import numpy as np
import plotly.graph_objs as go
X, Y, Z = np.mgrid[-3:3:10j, -5:5:10j, -1:1:3j]
values = np.random.randint(100, size=300)
fig = go.Figure(go.Volume(x=X.flatten(),
y=Y.flatten(),
z=Z.flatten(),
value=values.flatten(),
opacity=1,
surface_count=21))
fig.show()
values
However, when I input my 3D numpy array of shape (182,182,232)
and do the same, it does not do anything at all. No error, no figure plot (not even an empty one), nothing. But I have everything the same as in the example above, I define grid coordinates and then I input 1D flattened array as field values
import numpy as np
import plotly.graph_objs as go
import plotly.io as io
io.renderers.default='browser'
f = np.load('tomo.npy')
X, Y, Z = np.mgrid[-3:3:182j, -5:5:182j, -1:1:232j]
fig = go.Figure(data=go.Volume(
x=X.flatten(), y=Y.flatten(), z=Z.flatten(),
value=f.flatten(),
opacity=1,
surface_count=15,
))
fig.show()
I tried to change the renderer to the browser, and tried to do different min and max isosurfaces, to no avail. I am able to plot the 3D data with thus code:
pl = pv.Plotter()
pl.add_volume(tomo, cmap="coolwarm", opacity="sigmoid", shade=True)
pl.camera_position = [(929.0, 1067.0, -278.9), (249.5, 234.5, 101.25), (-0.2048, -0.2632, -0.9427)]
pl.camera.zoom(1.5)
pl.show()
So the data is not corrupted.