pythonmatplotlibstacked-bar-chartplot-annotations

How to make stacked bar chart with annotations


I have the following dataframe:

df = pd.DataFrame(data = {'id' : ['hbo', 'hbo', 'hbo', 'fox','fox','fox', 'NBC', 'NBC', 'NBC'], 
                     'device': ['laptop', 'tv', 'cellphone','laptop', 'tv', 'cellphone','laptop', 'tv', 'cellphone'], 
                     'count': [100,200,300, 400, 300, 200, 900, 1000, 100], 
                    'date': ['2022-04-30', '2022-05-30','2022-06-30', '2022-07-30', '2022-08-30','2022-09-30', 
                            '2022-10-30', '2022-11-30','2022-12-30']})

I am trying to code and make a stacked bar chart, with the following:

  1. Bar Chart is stacked by count, so like a normal stacked bar chart
  2. Annotation on the stacked bar chart areas showing the % (percentage)
  3. x-axis is to show the date
  4. y-axis to show count
  5. color is defined by device - e.g. tv = 'red'

The stacked bar chart is over time (as oppposed to line chart)

So far I have the following code, but am stuck on how to pivot the data and create the output using matplotlib:

fig, ax = plt.subplots(1,1)
ax = df.plot.bar(stacked=True, figsize=(10, 6), ylabel='count', xlabel='dates', title='count of viewing type over time', ax = ax)
plt.legend(title='Category', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.xlabel('Date', fontsize = 14)
plt.ylabel('Count of viewing type', fontsize = 14)
plt.title('plot test', fontsize = 20)
plt.xticks(rotation = 45)
plt.show();

Solution

  • This can done with Plotly express. I would first create a column with percentage values. Your dataset doesn't have multiple values per date but this will work in a larger dataset that does.

    df['pct'] = (df['count']/df.groupby('date')['count'].transform('sum')).apply(lambda x: '{:,.2%}'.format(x))
    

    Then create the figure. The barmode is 'relative' (aka stacked) by default so no need to specify for this question.

    import plotly.express as px
    
    fig = px.bar(df,
                 x='date',
                 y='count',
                 color='device',
                 #barmode='relative',
                 text='pct')
    
    fig.show()
    

    fig