提问人:mario DV 提问时间:11/16/2023 最后编辑:Shaidomario DV 更新时间:11/17/2023 访问量:50
带有 plotly 的多条轨迹的动画问题
Problem with animation of multiple traces with plotly
问:
我正在尝试用 Python 情节地为多个轨道制作动画。
代码为:
import pandas as pd
import plotly.express as px
df = pd.read_csv("orbite.csv")
fig=px.scatter(df,x="X1",y="Y1",animation_frame="time",range_x=[-6,1],range_y=[-5,2])\
fig.add_scatter(df,x="X2",y="Y2",animation_frame="time",range_x=[-6,1],range_y=[-5,2])
fig.layout.updatemenus[0].buttons[0].args[1]["frame"]["duration"] = 20
fig.update_layout(transition = {"duration": 20})
fig.show()
这是数据文件:
time X1 Y1 X2 Y2
0 1 -0.001000 0.000000 0.000000 10.000000
1 2 -0.001000 0.000000 -0.035000 9.940000
2 3 -0.000951 0.000049 -0.070000 9.890000
3 4 -0.000853 0.000148 -0.105000 9.830000
4 5 -0.000707 0.000297 -0.140000 9.780000
该程序只能使用一个动画即可正常工作,但是如果我添加 ,我会收到错误:fig.add_scatter()
The 'alignmentgroup' property is a string and must be specified as:
- A string
- A number that will be converted to a string
怎么了?
答:
0赞
Christian Karcher
11/17/2023
#1
add_scatter
不使用与 px.scatter 相同的功能(参见例如 https://stackoverflow.com/a/67529369/9501624),因此不能用于“组合动画”。
我所知道的唯一方法是根据graph_objects和帧“重建”新图形中的帧(参见例如,用plotly动画和叠加多个图):
import pandas as pd
import plotly.express as px
from plotly import graph_objects as go
data = {
"time": [1, 2, 3, 4, 5],
"X1": [0.5, 0.4, 0.3, 0.2, 0.1],
"Y1": [0.5, 0.4, 0.3, 0.2, 0.1],
"X2": [-0.5, -0.4, -0.3, -0.2, -0.1],
"Y2": [-0.5, -0.4, -0.3, -0.2, -0.1],
}
df = pd.DataFrame(data)
fig1 = px.scatter(df, x="X1", y="Y1", animation_frame="time")
fig2 = px.scatter(df, x="X2", y="Y2", animation_frame="time")
# build frames to be animated from two source figures.
frames = [
go.Frame(data=f.data + fig1.frames[i].data, name=f.name)
for i, f in enumerate(fig2.frames)
]
combined_fig = go.Figure(data=frames[0].data, frames=frames, layout=fig1.layout)
combined_fig.show()
评论
0赞
Christian Karcher
11/17/2023
很高兴听到它。请点击旁边的复选标记接受此答案,表明您收到了对您有用的答案。
评论