【发布时间】:2022-06-10 21:46:57
【问题描述】:
我有这段代码,它使用 plotly 为 NFL 跟踪数据设置动画。我希望能够手动更改主/客队和球的颜色。我尝试过使用下面的代码,但是它们都采用相同的颜色。我的数据集中有一个名为“颜色”的列,我想用它来为每个单独的数据点分配颜色。
# SCATTER TO ANIMATE TRACKING DATA
fig = px.scatter(data, x = "x", y = "y", hover_name="displayName",
color_discrete_sequence="color", animation_frame = "frameId", animation_group = "displayName",
range_y = [-5, 60], range_x = [-5, 125])
fig.layout.updatemenus[0].buttons[0].args[1]['frame']['duration'] = 80
for i in range(0, 121, 10):
fig.add_trace(go.Scatter(x=[i,i], y=[0,53.3], mode='lines', line=dict(color='white'), showlegend=False))
fig.update_layout(xaxis=dict(zeroline=False, showgrid=False),
yaxis=dict(zeroline=False, showgrid=False))
fig.add_trace(go.Scatter(x=[0,120], y=[0,0], mode='lines', line=dict(color='white'), showlegend=False))
fig.add_trace(go.Scatter(x=[0,120], y=[53.3,53.3], mode='lines', line=dict(color='white'), showlegend=False))
#fig.write_html("football.html")
fig.update_layout(
autosize=False,
width=1000,
height=600
)
fig
以下是数据示例:
| x | y | team | color | frameId | displayName |
|---|---|---|---|---|---|
| 61.21 | 46.77 | home | blue | 1 | Mahomes |
| 60 | 32 | away | red | 1 | Beckham |
还有一张我目前拥有的照片: Picture of my current Figure
【问题讨论】:
-
请分享一些文本格式的示例数据(不是图片)
-
我认为这个问题会受益于一张图片。您可以使用文本输入窗口顶部的工具栏插入图片文件,或按 Ctrl-G。
-
您好,感谢您的提示,我已经添加了一些示例数据和我目前拥有的图形的图片