我目前正在尝试使用Plotly的3D散点图制作动画。到目前为止,我已经成功地在一个图形中绘制了几个轨迹,并使用每帧一个轨迹创建了一个动画,但我无法将这两者结合起来。到目前为止,我的代码如下:
def animationTest(frames):
# Defining frames
frames=[
go.Frame(
data=[
go.Scatter3d(
x=trace.x,
y=trace.y,
z=trace.z,
line=dict(width=2)
)
for trace in frame],
)
for frame in frames]
# Defining figure
fig = go.Figure(
data=[
go.Scatter3d(
)
],
layout=go.Layout( # Styling
scene=dict(
),
updatemenus=[
dict(
type='buttons',
buttons=[
dict(
label='Play',
method='animate',
args=[None]
)
]
)
]
),
frames=frames
)
fig.show()
class TestTrace():
# 3D test data to test the animation
def __init__(self,ind):
self.x = np.linspace(-5.,5.,11)
self.y = ind*np.linspace(-5.,5.,11) # ind will change the slope
self.z = np.linspace(-5.,5.11)
fps = np.linspace(1.,10.,11)
inds = [-2,2]
#Slope changes for each frame for each trace so there's something to animate
frames = [[TestTrace(ind=f*ind) for ind in inds] for f in fps]
animationTest(frames)检查frames和frames[0].data的大小显示,我在单个帧中有正确的帧数和为data设置的轨迹数。但是,在生成的绘图中,只会显示具有第一个轨迹的第一帧,并且动画不会在单击播放时开始。任何帮助都将不胜感激。
发布于 2021-11-07 14:50:07
当上面的代码运行时,plotly似乎只绘制在go.Figure()对象的初始化中指定的绘图量,即使您将每帧的绘图量更多地传递到frames参数中也是如此。至少我是这么认为的。
下面是更新后的代码:
...
fig = go.Figure(
data=[
go.Scatter3d(
)
for traces in frames[0]], # This has to match the number of plots that will be passed to frames
...这个问题在Plotly: Animating a variable number of traces in each frame in R中也有一些概述,如果帧的轨迹少于初始化时的轨迹,则必须存在虚拟轨迹。底线是,传递的列表的长度很重要。
https://stackoverflow.com/questions/69867334
复制相似问题