Lynn
Lynn

Reputation: 4398

Plotly chart not animating properly in Python

I have a same dataframe, df, that I have styled and created a line chart in Plotly. I have included an animation feature, yet the chart seems to be giving an error.

   Size     Date        POD
   0.027289 11/1/2020   S 
   0.013563 11/1/2020   S 
   0.058498 11/1/2020   S 
   0.281953 11/1/2020   S 
   0.479725 11/1/2020   S 
   0.007358 11/1/2020   S 
   0.075818 11/1/2020   S 
   0.069744 11/1/2020   S 
   0.029844 11/1/2020   S 
   0.38383  11/1/2020   S 
   0.3451   11/1/2020   S 
   0.034024 11/1/2020   S 
   0.292939 11/1/2020   S 
   0.208562 11/1/2020   S 
   0.013108 11/1/2020   S 

This is what I am doing

 import plotly.express as px
 import plotly.graph_objects as go


 fig = px.scatter(df1, x = "Date", y = "Size", color = "POD",title = "POD Growth in US", 
 animation_frame="Date",
 log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90],labels = {"Size": "Size in TB"})

 fig.update_layout(
 font_family="Arial",
 font_color="black",
 title_font_family="Arial",
 title_font_color="black",
 legend_title_font_color="black"
  )


fig.update_xaxes(title_font_family="Arial")

fig.update_layout(
title={
    'text': "POD Growth in US",
    'y':0.9,
    'x':0.5,
    'xanchor': 'center',
    'yanchor': 'top'})

 fig.update_traces(marker=dict(size=8,
    line=dict(width=1,
    color='DarkSlateGrey')),
    selector=dict(mode='markers'))

fig.show()

I think the problem lies within this line of code here but I am not 100% sure with what I am doing wrong:

 animation_frame="Date",
 log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90]

The error I get:

  Invalid value of type 'pandas._libs.tslibs.timestamps.Timestamp' received for the 'name' property 
  of frame
  Received value: Timestamp('2020-09-01 00:00:00
  The 'name' property is a string and must be specified as:
  - A string
  - A number that will be converted to a string

Would I have to do a conversion to the "Date" column to resolve the issue?

Here is the graph I am wanting to animate:

enter image description here

Upvotes: 2

Views: 934

Answers (1)

Derek O
Derek O

Reputation: 19565

I am not sure if your candlestick animation will run as intended, but changing the Date column of your DataFrame from a Timestamp to a string (using the datetime library) should resolve the error.

import datetime as dt

df1['Date'] = df1['Date'].map(lambda x: x.strftime('%m/%d/%Y'))

Upvotes: 1

Related Questions