如何在Plotlypython中制作基于列类的条形图?

问题描述 投票:0回答:1

我有Dataframe

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

首先,我将我的日期和Category与Sum分组,这样我就得到了这样的数据框架。

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中显示 "25M"、"50M "和 "100M "三个条形的日期。

python-3.x plotly plotly-dash plotly-python
1个回答
1
投票

我想你应该要用 标准条形图 并添加一个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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.