Plotly 体积帧(四维切片)

Plotly volume frames (four-dimensional slices)

能否this example适应显示三维切片而不是二维切片?

我尝试用 Volume 替换 Surface:

a, b, c = data.shape[1:]  # data.ndim == 4
X, Y, Z = np.mgrid[-1:1:a*1j, -1:1:b*1j, -1:1:c*1j]

fig = go.Figure(frames=[go.Frame(data=go.Volume(
    x=X.flatten(),
    y=Y.flatten(),
    z=Z.flatten(),
    value=data[k].flatten(),
    opacity=.25,
    surface_count=12,
    colorscale='jet',
    ), name=str(k)) for k in range(len(data))])

并且 Volume 本身工作正常,但我得到了上面的空帧。

感谢@empet Plotly Community:

import numpy as np
import plotly.graph_objects as go
from plotly.offline import plot  # for IDE use

vol4 = np.random.randint(0, 27, size=(6, 5, 7, 4))

a, b, c = vol4.shape[1:]
X, Y, Z = np.mgrid[-1:1:a*1j, -1:1:b*1j, -1:1:c*1j]

fig = go.Figure(go.Volume(x=X.flatten(),
                          y=Y.flatten(),
                          z=Z.flatten(),
                          value=vol4[0].flatten(),
                          opacity=.25,
                          surface_count=12,
                          colorscale='turbo',
                          colorbar_len=0.8
    ))

frames=[go.Frame(data=go.Volume(
                               value=vol4[k].flatten()), 
                 name=str(k)) for k in range(len(vol4))]
updatemenus = [dict(
        buttons = [
            dict(
                args = [None, {"frame": {"duration": 100, "redraw": True},
                                "fromcurrent": True, "transition": {"duration": 0}}],
                label = "Play",
                method = "animate"
                ),
            dict(
                 args = [[None], {"frame": {"duration": 0, "redraw": False},
                                  "mode": "immediate",
                                  "transition": {"duration": 0}}],
                label = "Pause",
                method = "animate"
                )
        ],
        direction = "left",
        pad = {"r": 10, "t": 87},
        showactive = False,
        type = "buttons",
        x = 0.21,
        xanchor = "right",
        y = -0.075,
        yanchor = "top"
    )] 

sliders = [dict(steps = [dict(method= 'animate',
                              args= [[f'{k}'],                           
                              dict(mode= 'immediate',
                                   frame= dict(duration=100, redraw=True),
                                   transition=dict(duration= 0))
                                 ],
                              label=f'{k+1}'
                             ) for k in range(len(vol4))], 
                active=0,
                transition= dict(duration=0),
                x=0, # slider starting position  
                y=0, 
                currentvalue=dict(font=dict(size=12), 
                                  prefix='frame: ', 
                                  visible=True, 
                                  xanchor= 'center'
                                 ),  
                len=1.0) #slider length
           ]

fig.update_layout(width=700, height=700, updatemenus=updatemenus, sliders=sliders)
fig.update(frames=frames)
plot(fig, auto_open=True)