0
votes

How to plot a stacked bar graph of the count of the number of "Major /Minor" issues at any point of time?

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

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 Series Output:

Issue_Date     Severity
20.2.2020       Major     1
                Minor     1
31.3.2020       Major     2
                Minor     1
01.4.2020       Major     1

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?

1
What have you tried so far?Bertil Johannes Ipsen

1 Answers

0
votes

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)