【问题标题】:How can I change the color of my scatter plot points based on a "color" column in my dataset in plotly (python)?如何根据我的数据集中的“颜色”列更改散点图点的颜色(python)?
【发布时间】: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。
  • 您好,感谢您的提示,我已经添加了一些示例数据和我目前拥有的图形的图片

标签: python animation plotly


【解决方案1】:

color_discrete_sequence="color" 应该只是 color="color"

应该是这样

# SCATTER TO ANIMATE TRACKING DATA 
fig = px.scatter(data,  x = "x", y = "y", hover_name="displayName",
          color="color", animation_frame = "frameId", animation_group = "displayName",
          range_y = [-5, 60], range_x = [-5, 125])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-11
    • 2015-09-18
    • 1970-01-01
    • 2015-04-17
    • 2017-09-14
    • 2013-10-08
    • 2014-10-09
    • 2022-01-13
    相关资源
    最近更新 更多