简体   繁体   English

在 Plotly、Python 中具有时间 slider 的多个连续条形图

[英]Multiple consecutive bar plots with a time slider in Plotly, Python

I have a Pandas dataframe representing portfolio weights in multiple dates, such as the following contents in CSV format:我有一个 Pandas dataframe 代表多个日期的投资组合权重,例如 CSV 格式的以下内容:

DATE,ASSET1,ASSET2,ASSET3,ASSET4,ASSET5,ASSET6,ASSET7
2010-01-04,0.250000,0.0,0.250000,0.000000,0.25,0.000000,0.250000
2010-02-03,0.250000,0.0,0.250000,0.000000,0.25,0.000000,0.250000
2010-03-05,0.217195,0.0,0.250000,0.032805,0.25,0.000000,0.250000
2010-04-06,0.139636,0.0,0.250000,0.110364,0.25,0.000000,0.250000
2010-05-05,0.179569,0.0,0.218951,0.101480,0.25,0.000000,0.250000
2010-06-04,0.207270,0.0,0.211974,0.080756,0.25,0.000000,0.250000
2010-07-06,0.132468,0.0,0.250000,0.117532,0.25,0.000000,0.250000
2010-08-04,0.116353,0.0,0.250000,0.133647,0.25,0.000000,0.250000
2010-09-02,0.081677,0.0,0.250000,0.168323,0.25,0.000000,0.250000
2010-10-04,0.000000,0.0,0.250000,0.250000,0.25,0.009955,0.240045

For each row in the Pandas dataframe resulting from this CSV, we can generate a bar chart with the portfolio composition at that day.对于由此 CSV 产生的 Pandas dataframe 中的每一行,我们可以生成包含当天投资组合构成的条形图。 I would like to have multiple bar charts, with a time slider, such that we can choose one of the dates and see the portfolio composition during that day.我想要多个条形图,时间为 slider,这样我们就可以选择其中一个日期并查看当天的投资组合构成。

Can this be achieved with Plotly?这可以用 Plotly 实现吗?

I could not find a way to do it straight in the dataframe above, but it is possible to do it by "melting" the dataframe.我无法在上面的 dataframe 中直接找到方法,但可以通过“熔化”dataframe 来实现。 The following code achieves what I was looking for, together with some beautification of the chart:以下代码实现了我正在寻找的内容,以及对图表的一些美化:

import pandas as pd
from io import StringIO
import plotly.express as px

string = """
DATE,ASSET1,ASSET2,ASSET3,ASSET4,ASSET5,ASSET6,ASSET7
2010-01-04,0.250000,0.0,0.250000,0.000000,0.25,0.000000,0.250000
2010-02-03,0.250000,0.0,0.250000,0.000000,0.25,0.000000,0.250000
2010-03-05,0.217195,0.0,0.250000,0.032805,0.25,0.000000,0.250000
2010-04-06,0.139636,0.0,0.250000,0.110364,0.25,0.000000,0.250000
2010-05-05,0.179569,0.0,0.218951,0.101480,0.25,0.000000,0.250000
2010-06-04,0.207270,0.0,0.211974,0.080756,0.25,0.000000,0.250000
2010-07-06,0.132468,0.0,0.250000,0.117532,0.25,0.000000,0.250000
2010-08-04,0.116353,0.0,0.250000,0.133647,0.25,0.000000,0.250000
2010-09-02,0.081677,0.0,0.250000,0.168323,0.25,0.000000,0.250000
2010-10-04,0.000000,0.0,0.250000,0.250000,0.25,0.009955,0.240045
"""

df = pd.read_csv(StringIO(string))

df = df.melt(id_vars=['DATE']).sort_values(by = 'DATE')

fig = px.bar(df, x="variable", y="value", animation_frame="DATE")
fig.update_layout(legend_title_text = None)
fig.update_xaxes(title = "Asset")
fig.update_yaxes(title = "Proportion")
fig.update_layout(autosize = True, height = 600)
fig.update_layout(hovermode="x")
fig.update_layout(plot_bgcolor="#F8F8F8")
fig.update_traces(
hovertemplate=
    '<i></i> %{y:.2%}'
)
fig.show()

This produces the following:这会产生以下结果:

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM