带有颜色编码的动画散点图会松散颜色(Python / Plotly)

时间:2020-08-14 17:53:23

标签: python animation plotly

我有一个4D数据集,其中3个维度描述了数据,第四个维度是时间索引。使用第3维的颜色编码将数据集绘制为散点图可以正常工作。当我尝试使用第四维对其进行动画处理时,颜色编码将停止工作。由于该图表应易于阅读,因此使用plot.ly与之交互。

这里是3D方法:

import pandas as pd
import plotly.express as px

dat={'1.Value':[1,2,3,4,5,6,7,8,9],
     '2.Value':[9,8,7,6,5,4,3,2,1],
     '3.Value':[10,11,13,15,10,10,15,17,18],
     '4.Value':[1,2,3,4,5,6,7,8,9]}

df = pd.DataFrame(data=dat)

fig = px.scatter(df, x="2.Value", y="3.Value",
           color="4.Value" ,range_x=[0,20], range_y=[0,20]
          )
fig.show()

这里是动画时间的方法。请注意,颜色编码消失了:

fig = px.scatter(df, x="2.Value", y="3.Value", animation_frame="1.Value", 
           color="4.Value" ,range_x=[0,20], range_y=[0,20]
          )
fig.show()

任何人是否有这一切的工作方式和/或工作方式。如果答案很重要,则代码将在jupyter笔记本中执行。

1 个答案:

答案 0 :(得分:0)

我可以建议将点大小而不是动画添加为另一个维度。

fig = px.scatter(
    df, 
    x="2.Value", 
    y="3.Value",
    color="4.Value",
    size="1.Value",
    range_x=[0,20], 
    range_y=[0,20]
)
fig.show()

enter image description here