1

Hey I was trying to make an line animation for the stocks data built in plotly. So I tried this code below following https://plotly.com/python/animations/ but nothing shows.

import plotly.express as px
df = px.data.stocks()
fig = px.line(df, x = 'date', y=df.columns[1:6], animation_frame = 'date')
fig.show()

what I intended to do was to make a line animation of the 6 company's stock price with respect to the date. I'm totally new to plotly so this maybe a dumb question but I'd be grateful if you guys could help. Thank You!

wyc
  • 11
  • 2

1 Answers1

1
  • you need some consistency across the animation frames for the xaxis and yaxis
  • to achieve this I modified to use day of month as xaxis and ensured range is appropriate for all frames in yaxis
  • then used month/year as the animation (a line only makes sense if there is more that one value to plot) so there are a collection of values in each frame
import plotly.express as px
import pandas as pd

df = px.data.stocks()
df["date"] = pd.to_datetime(df["date"])
fig = px.line(df, x = df["date"].dt.day, y=df.columns[1:6], animation_frame=df["date"].dt.strftime("%b-%Y"))
fig.update_layout(yaxis={"range":[0,df.iloc[:,1:6].max().max()]})
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30