简体   繁体   English

如何使用 plotly 为股票数据生成一行 animation?

[英]How can I generate a line animation for the stocks data using plotly?

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.嘿,我试图为 plotly 中内置的股票数据创建一行 animation。所以我在https://plotly.com/python/animations/之后尝试了以下代码,但没有任何显示。

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.我打算做的是将 6 家公司的股票价格与日期相关联,形成一条线 animation。 I'm totally new to plotly so this maybe a dumb question but I'd be grateful if you guys could help.我是 plotly 的新手,所以这可能是一个愚蠢的问题,但如果你们能提供帮助,我将不胜感激。 Thank You!谢谢你!

  • you need some consistency across the animation frames for the xaxis and yaxis对于 xaxis 和 yaxis,您需要在 animation 帧之间保持一定的一致性
  • to achieve this I modified to use day of month as xaxis and ensured range is appropriate for all frames in yaxis为了实现这一点,我修改为使用月中的某天作为 xaxis 并确保范围适用于 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然后使用月/年作为 animation(只有当要绘制的值不止一个时,一条线才有意义)因此每个帧中都有一组值
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()]})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用股票数据对 multiIndex dataframe 进行切片? - How can I slice a multiIndex dataframe with stocks data? 我如何使用 plotly plot 置信区间为 python 的线? - How can I plot a line with a confidence interval in python using plotly? Plotly:如何使用折线图的下拉列表更新 plotly 数据? - Plotly: How to update plotly data using dropdown list for line graph? 如何为 Plotly express 动画打开自动播放? (Python) - How can I turn on autoplay for a Plotly express animation? (Python) Plotly:如何在 plotly express 中使数据在动画帧之间保持一致(即避免消失的数据) - Plotly: How to make data consistent across animation frames (i.e. avoiding vanishing data) in plotly express 如何强制 Plotly 动画从最后一帧开始? - How can I force Plotly animation to start by the last frame? 如何在破折号 plotly 中绘制此数据? - How can I graph this data in dash plotly? 我可以一次性为所有列(持有股票收益)生成 Beta(股票),而不必遍历 Pandas 框架中的每一列 - Can I generate Beta(of stocks) for all columns (holding stocks returns) at one go without having to loop through each column in a Pandas frame Plotly 如何创建一行 animation,x 轴为列名称,y 轴为列数据? - Plotly How to create a line animation with column name in x axis and column data in y axis? 如何在 plotly 中向散点图添加一条线? - How can I add a single line to a scatter plot in plotly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM