Home > Software design >  Annotate a normalized barchart with original data
Annotate a normalized barchart with original data

Time:01-20

I have a dataframe consisting of;

home    away    type
0   0.0 0.0 reds
1   5.0 1.0 yellows
2   7.0 5.0 corners
3   4.0 10.0    PPDA
4   5.0 1.0 shots off
5   7.0 5.0 shots on
6   1.0 1.0 goals
7   66.0    34.0    possession

to get the stacked bar chart I wanted, I normalized the data using

stackeddf1 = df1.iloc[:,0:2].apply(lambda x: x*100/sum(x),axis=1)

and then I create my barchart using

ax = stackeddf1.iloc[1:, 0:2].plot.barh(align='center', stacked=True, figsize=(20, 20),legend=None)
for p in ax.patches:
    width, height = p.get_width(), p.get_height()
    x, y = p.get_xy() 
    ax.text(x width/2, 
            y height/2, 
            '{:.0f}'.format(width), 
            horizontalalignment='center', 
            verticalalignment='center')

This though, annotates the barchart with the new normalized data. If possible I'd like to find a way to use my original to annotate.

enter image description here

CodePudding user response:

You can use matplotlib's new bar_label function together with the values of the original dataframe:

import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
import pandas as pd
import numpy as np

df = pd.DataFrame({'home': np.random.randint(1, 10, 10),
                   'away': np.random.randint(1, 10, 10),
                   'type': [*'abcdefghij']})
df_normed = df.set_index('type')
df_normed = df_normed.div(df_normed.sum(axis=1), axis=0).multiply(100)
ax = df_normed.plot.barh(stacked=True, width=0.9, cmap='turbo')
for bars, col in zip(ax.containers, df.columns):
    ax.bar_label(bars, labels=df[col], label_type='center', fontsize=15, color='yellow')
ax.legend(loc='upper left', bbox_to_anchor=(1.01, 1))
for sp in ['top', 'right']:
    ax.spines[sp].set_visible(False)
ax.xaxis.set_major_formatter(PercentFormatter())
ax.margins(x=0)
plt.tight_layout()
plt.show()

bars with bar_label from different dataframe

  • Related