Home > Blockchain >  Stacked bar plot for items with multiple identifiers across dates using plotly
Stacked bar plot for items with multiple identifiers across dates using plotly

Time:06-16

I have the following data frame

sensor_id u_code date_time
1234abcd 4 13-06-2022 18:02
5678efgh 4 13-06-2022 18:02
1234abcd 2 13-06-2022 18:03
5678efgh 2 13-06-2022 18:03
1234abcd 6 13-06-2022 18:04
5678efgh 6 13-06-2022 18:04
1234abcd 10 13-06-2022 18:05
5678efgh 10 13-06-2022 18:05
1234abcd 4 13-06-2022 18:06
5678efgh 6 13-06-2022 18:06
1234abcd 4 13-06-2022 18:07
5678efgh 6 13-06-2022 18:07
1234abcd 2 13-06-2022 18:08
5678efgh 2 13-06-2022 18:08
1234abcd 10 13-06-2022 18:09
5678efgh 10 13-06-2022 18:09
1234abcd 4 14-06-2022 18:02
5678efgh 6 14-06-2022 18:02
1234abcd 2 14-06-2022 18:03
5678efgh 2 14-06-2022 18:03
1234abcd 2 14-06-2022 18:04
5678efgh 2 14-06-2022 18:04
1234abcd 4 14-06-2022 18:05
5678efgh 4 14-06-2022 18:05
1234abcd 10 14-06-2022 18:06
5678efgh 10 14-06-2022 18:06
1234abcd 6 14-06-2022 18:07
5678efgh 6 14-06-2022 18:07
1234abcd 2 14-06-2022 18:08
5678efgh 2 14-06-2022 18:08
1234abcd 4 14-06-2022 18:09
5678efgh 4 14-06-2022 18:09

I would like to make an interactive stacked bar plot using plotly which looks like below.

expected stacked bar plot

I am used to making stacked bar plots in plotly. But here my challenge is to have separate stacked bar plots (one per sensor_id) on each date.

CodePudding user response:

This cannot be accomplished by express alone, as it requires both stacked and grouped bar charts. I am not sure if the method I have employed is optimal: I am reusing the graph data from express and creating a graph with the results extracted by sensor id in the graph object. As for the data, I have grouped and aggregated data frames for the graph. Sensor names are added as annotations. Add to both x-axis bar graphs if needed. The legend is displayed as a color bar and that is hidden. As a complement to that functionality, code names are added as annotations to the graphs.

df['date_time'] = pd.to_datetime(df['date_time'], format='%d-%m-%Y %H:%M')
df['date'] = df['date_time'].dt.date
dfg = df.groupby(['date','sensor_id','u_code']).size().to_frame('counts')
dfg.reset_index(inplace=True)

import plotly.express as px
import plotly.graph_objects as go

figA = px.bar(dfg.query('sensor_id == "1234abcd"'), x="date", y="counts", color="u_code", text="u_code", barmode='stack')
figB = px.bar(dfg.query('sensor_id == "5678efgh"'), x="date", y="counts", color="u_code", text="u_code", barmode='stack')

fig = go.Figure()
fig.add_trace(go.Bar(figA.data[0]))
fig.add_trace(go.Bar(figB.data[0]))

fig.add_annotation(x=dfg['date'].unique()[0], y=8.5,
                   xref='x', yref='y',
                   text="1234abcd",
                   showarrow=False,
                   xshift=-90)

fig.add_annotation(x=dfg['date'].unique()[0], y=8.5,
                   xref='x', yref='y',
                   text="5678efgh",
                   showarrow=False,
                   xshift=90)

fig.update_layout(yaxis=dict(range=[0,10]), coloraxis_showscale=False)

fig.show()

enter image description here

  • Related