我有数据框
Date Category Sum
2019-06-03 "25M" 34
2019-06-03 "25M" 60
2019-06-03 "50M" 23
2019-06-04 "25M" 67
2019-06-05 "50M" -90
2019-06-05 "50M" 100
2019-06-06 "100M" 6
2019-06-07 "25M" -100
2019-06-08 "100M" 67
2019-06-09 "25M" 450
2019-06-10 "50M" 600
2019-06-11 "25M" -9
2019-07-12 "50M" 45
2019-07-13 "50M" 67
2019-07-14 "100M" 130
2019-07-14 "50M" 45
2019-07-15 "100M" 100
2019-07-16 "25M" -90
2019-07-17 "25M" 700
2019-07-18 "25M" -9
首先,我将日期和类别与总和分组这样我就得到了这样的数据框。
df.groupby(["EntryDate","Category"]).agg({"NetProfit":"sum"}).reset_index()
Date Category Sum
2019-06-03 "25M" 94
2019-06-03 "50M" 23
2019-06-04 "25M" 67
2019-06-05 "50M" 10
2019-06-06 "100M" 6 ...and so on
[我想创建绘制时间序列分组的条形图以便在PLotly中分别显示3个分别代表“ 25M”,“ 50M”和“ 100M”的条。
我认为您想使用standard bar chart并为25M,50M和100M添加一个条目,例如:
import plotly.graph_objects as go
fig = go.Figure(data=[
go.Bar(name='25M', x=df.EntryDate[df.Category=='25M'], y=df.NetProfit[df.Category=='25M']),
go.Bar(name='50M', x=df.EntryDate[df.Category=='50M'], y=df.NetProfit[df.Category=='50M']),
go.Bar(name='100M', x=df.EntryDate[df.Category=='100M'], y=df.NetProfit[df.Category=='100M'])
])
fig.show()
或循环中:
import plotly.graph_objects as go
AMT=['25M','50M','100M']
fig = go.Figure()
for i in AMT:
fig.add_trace(go.Bar(name=i,
x=df.EntryDate[df.Category==i],
y=df.NetProfit[df.Category==i],
)
)
fig.show()