Animated scatterplot with colorcoding looses color (Python/ Plotly)

Viewed 162

I have a 4D dataset with 3 dimensions describing the data and the fourth a time-index. Plotting the dataset as a scatterplot with colorcoding for the 3rd dimension works fine. When I try to animate it using the fourth dimension the color coding stops to work. As the chart should be easy to read, plot.ly is used for interaction with it.

Here the 3D approach:

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()

And here the approach with animated time. Note that the colorcoding disappears:

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()

Does anyone have any whether this works at all and/or how it works. If it matters for the answer the code is executed in a jupyter notebook.

1 Answers

May i suggest adding point size as another dimension instead of animation.

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

Related