【发布时间】:2021-12-29 19:16:11
【问题描述】:
我正在尝试创建一个动画线图来说明 3 个不同资产类别随时间(年)的价格上涨,但它不起作用,我不知道为什么!
到目前为止我做了什么:
- 获取每个资产的收盘价数据
start = datetime.datetime(2010,7,01)
end = datetime.datetime(2021,7,01)
data = pdr.get_data_yahoo(['BTC-USD', 'GC=F','^GSPC'],startDate,endDate)['Adj Close']
- 将列转置为行以避免大量计算
data['Date'] = data.index
data['Year'] = data.index.year
dataNew =data.melt(['Date', 'Year'], var_name='Asset')
dataNew = dataNew.rename(columns = {'value': 'Price'})
- 情节
fig = px.line(dataNew,
x = 'Date',
y = 'Price',
range_y=[0,50000],
color = 'Asset',
animation_frame = 'Year')
st.write(fig)
输出:
【问题讨论】:
-
帖子Cumulative Lines Animation in Python 表明使用Python 和Plotly.Express 是不可能的,而使用
go.Figure()进行多重跟踪是最可行的方法。 -
也有一个例子here,但它并不完全直截了当,也没有使用 Plotly Express。
标签: python animation plotly plotly-python plotly-express