【问题标题】:Plotly volume frames (four-dimensional slices)Plotly 体积帧(四维切片)
【发布时间】:2021-07-23 19:30:23
【问题描述】:

this example 可以适应显示三维切片而不是二维切片吗?

我尝试将Surface 替换为Volume

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 本身可以正常工作,但我得到了上面的空帧。

【问题讨论】:

    标签: python plotly-dash plotly-python


    【解决方案1】:

    感谢@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)
    

    【讨论】:

      猜你喜欢
      • 2014-09-19
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2018-09-04
      • 2020-01-21
      相关资源
      最近更新 更多