问题描述
如何绘制任意时间点主要/次要"问题数量的堆积条形图?
How to plot a stacked bar graph of the count of the number of "Major /Minor" issues at any point of time?
我在 csv 文件中有如下数据:
I have data in csv file as follows:
Issue_Date Severity
20.2.2020 Major
20.2.2020 Minor
31.3.2020 Major
31.3.2020 Major
31.3.2020 Minor
01.4.2020 Major
我正在使用 Pandas 数据框读取上述 CSV,并尝试使用 group by 方法计算给定日期特定严重性的发生率
I am reading the above CSV using pandas data frame and I tried to count the occurrence of particular Severity on a given date using group by method
data = df.groupby(["Issue_Date", "Severity"]).size()
这里的数据"是一个系列输出:
Here "data" is a SeriesOutput:
Issue_Date Severity
20.2.2020 Major 1
Minor 1
31.3.2020 Major 2
Minor 1
01.4.2020 Major 1
在 xaxis 上显示 Issue_date,在 yaxis 上显示计数并根据严重性绘制堆叠类别.
On xaxis display Issue_date and on yaxis display counts and plot stacked categories on the basis of severity.
如何使用破折号来实现它?
How can I achieve it using dash plotly?
推荐答案
您需要取消堆叠 groupby
并将它们作为 2 条轨迹添加到绘图中.这是一个工作示例:
You need to unstack the groupby
and add them as 2 traces to the plotly graph.Here is a working example:
import pandas as pd
import plotly.graph_objects as go
import dash
import dash_core_components as dcc
import dash_html_components as html
data = {'Issue_Date': ['20.2.2020','20.2.2020','31.3.2020','31.3.2020','31.3.2020','01.4.2020'],
'Severity': ['Major','Minor','Major' ,'Major' ,'Minor','Major']}
df = pd.DataFrame (data, columns = ['Issue_Date', 'Severity'])
temp = df.groupby(["Issue_Date", "Severity"]).size()
temp1 = temp.rename('size').reset_index()
major=temp1[temp1['Severity']=='Major']
minor=temp1[temp1['Severity']=='Minor']
fig = go.Figure(data=[
go.Bar(name='Major', x=major['Issue_Date'], y=major['size']),
go.Bar(name='Minor', x=minor['Issue_Date'], y=minor['size'])
])
fig.update_layout(barmode='stack')
app = dash.Dash()
app.layout = html.Div([
dcc.Graph(figure=fig)
])
app.run_server(debug=False)
这篇关于如何在特定日期绘制带有类别计数的 Dash 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!