Why slider not work?

I want to create an animation of a cone move along a curve using sliders. While the button ‘play’ works fine, why doesn’t the slider function well?

Here is code:

import plotly.graph_objs as go
import numpy as np

# 定义圆锥体的参数
height = 1
radius = 0.5
resolution = 50

# 生成圆锥体的数据
z = np.linspace(0, height, resolution)
theta = np.linspace(0, 2 * np.pi, resolution)
z, theta = np.meshgrid(z, theta)
x = (height - z) / height * radius * np.cos(theta)
y = (height - z) / height * radius * np.sin(theta)

# 生成运动轨迹数据
t = np.linspace(0, 10, 100)
path_x = np.sin(t)
path_y = np.cos(t)
path_z = t / 10

# 创建帧数据
frames = []
for i in range(len(t)):
    frame_data = go.Surface(
        x=x + path_x[i],
        y=y + path_y[i],
        z=z + path_z[i],
        colorscale='Viridis',
        opacity=0.8
    )
    frames.append(go.Frame(data=[frame_data]))

# 创建初始位置的圆锥体
initial_trace = go.Surface(
    x=x + path_x[0],
    y=y + path_y[0],
    z=z + path_z[0],
    colorscale='Viridis',
    opacity=0.8
)

# 创建轨迹线
trace_path = go.Scatter3d(
    x=path_x,
    y=path_y,
    z=path_z,
    mode='lines',
    line=dict(color='black', width=2),
    name='Path'
)

# 创建布局
layout = go.Layout(
    title='Moving Cone in 3D Space',
    scene=dict(
        xaxis=dict(title='X-axis'),
        yaxis=dict(title='Y-axis'),
        zaxis=dict(title='Z-axis')
    ),
    updatemenus=[{
        'buttons': [
            {
                'args': [None, {'frame': {'duration': 50, 'redraw': True}, 'fromcurrent': True}],
                'label': 'Play',
                'method': 'animate'
            },
            {
                'args': [[None], {'frame': {'duration': 0, 'redraw': False}, 'mode': 'immediate'}],
                'label': 'Pause',
                'method': 'animate'
            }
        ],
        'direction': 'left',
        'pad': {'r': 10, 't': 87},
        'showactive': False,
        'type': 'buttons',
        'x': 0.1,
        'xanchor': 'right',
        'y': 0,
        'yanchor': 'top'
    }],
    sliders=[{
        'steps': [
            {
                'method': 'animate', 
                'args': [
                    [f'frame{k}'], 
                    {
                        'mode': 'immediate', 
                        'frame': {'duration': 50, 'redraw': True}, 
                        'transition': {'duration': 0}
                    }], 
                'label': str(k)
            } 
            for k in range(len(t))],
        'transition': {'duration': 0},
        'x': 0.1,
        'xanchor': 'left',
        'y': -0.1,
        'yanchor': 'top',
        'currentvalue': {'prefix': 'Time: ', 'font': {'size': 20}},
        'len': 0.9
    }]
)

# 创建图表
fig = go.Figure(data=[initial_trace, trace_path], layout=layout, frames=frames)

# 显示图表
fig.show()

Because frame name is not specified in frames.
corrected code:

# 创建帧数据
frames = []
for i in range(len(t)):
    frame_data = go.Surface(
        x=x + path_x[i],
        y=y + path_y[i],
        z=z + path_z[i],
        colorscale='Viridis',
        opacity=0.8
    )
    frames.append(go.Frame(data=[frame_data], name=f'frame{i}'))
1 Like