我已经搜索了合适的动画脚本,并提出了以下关于如何添加痕迹的函数:
from plotly import graph_objects as go
def add_anim_frames(figure: go.Figure, traces):
sliders_dict = {
"active": 0,
# "yanchor": "top",
# "xanchor": "left",
"currentvalue": {
"font": {"size": 20},
"prefix": "",
"visible": True,
"xanchor": "right"
},
"transition": {"duration": 300, "easing": "cubic-in-out"},
"len": 0.9,
# "x": 0.1,
# "y": 0,
"steps": [],
'pad': dict(zip('lrtb', [0, 0, 0, 0])),
}
frames = [
go.Frame(data=trace_list,
traces=list(range(len(trace_list))),
name=str(i))
for i, trace_list in enumerate(traces)
]
f_debug = frames[0]
print(len(f_debug.data))
print(f_debug.traces)
sliders_dict['steps'] = [
{"args": [[fr.name],
{"frame": {"duration": 0, "redraw": True},
"mode": "immediate",
"transition": {"duration": 0}}],
"label": i,
"method": "animate"} for i, fr in enumerate(frames)]
figure.frames = frames
figure.layout.updatemenus = [
go.layout.Updatemenu(
type='buttons',
buttons=[
go.layout.updatemenu.Button(
label="Play",
method="animate",
args=[
None,
{'frame': {"duration": 50, "redraw": True},
"mode": "immediate",
"fromcurrent": True,
"transition": {"duration": 50, "easing": "linear"}},
]),
go.layout.updatemenu.Button(
label='Pause',
method='animate',
args=[
[None],
{"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}
]),
],
bgcolor='rgba(0, 0, 0, 0)', active=99, bordercolor='black',
font=dict(size=11, color='white', ), showactive=False,
**{
"direction": "right",
'pad': dict(zip('lrtb', [0, 0, 0, 0])),
"x": 0.5,
"xanchor": "right",
"y": 1,
"yanchor": "top"
}
),
]
figure.layout.sliders = [sliders_dict]
我这样展示我的身材:
import numpy as np
from plotly import graph_objects as go
cube_points = np.zeros((8, 3))
cube_points[[2, 3, 4, 5], 0] = 1
cube_points[[1, 2, 5, 6], 1] = 1
cube_points[[4, 5, 6, 7], 2] = 1
N_FRAMES = 40
anim_points = np.stack([
np.sin(np.arange(N_FRAMES)),
np.cos(np.arange(N_FRAMES)),
np.arange(N_FRAMES)/10
], axis=-1)
static_traces = [
go.Scatter3d(**dict(zip('xyz', cube_points.T*5)), mode='markers', name='static'),
]
anim_traces = [
[
go.Scatter3d(**dict(zip('xyz', anim_points[[i-1, i, i+1]].T + 2.5)), mode='lines', marker_color='lime'),
go.Scatter3d(**dict(zip('xyz', anim_points[[i-1, i, i+1]].T + 2.7)), mode='lines', marker_color='olive'),
go.Scatter3d(**dict(zip('xyz', anim_points[[i-1, i, i+1]].T+2.3)), mode='lines', marker_color='blue'),
]
for i in range(1, N_FRAMES-1)]
fig = go.Figure(data=[go.Scatter3d()])
fig.add_traces(static_traces)
fig.update_layout(plt.get_layout_3d(show_grid=True), height=700)
add_anim_frames(fig, anim_traces)
fig.show()
问题是
Frame
对象似乎一次只能显示 2 条轨迹:
如果我在动画跟踪列表中只留下 1 个跟踪,则将显示静态跟踪,如果我添加额外的 enim 跟踪,则仅显示动画跟踪,如果我添加更多,则仅显示前 2 个动画跟踪。请帮忙
我发现了一个肮脏的生活窍门,你需要预先在图中制作占位符痕迹,并一次显示痕迹数量,如下所示:
N_DISPLAYED = 4
fig = go.Figure()
fig.add_traces([go.Scatter3d()]*N_DISPLAYED)