
# https://www.globalsino.com/ICs/
# Horizontal stacked bar

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# data
labels = ['A', 'B', 'C', 'D', 'E']    
TeamA = [1, 45, 28, 11, 4]
TeamB = [3, 58, 17, 8, 3]
TeamC = [1, 44, 30, 11, 3]

# create a dict with the keys as the desired legend labels
data = {'labels': labels, 'TeamA': TeamA, 'TeamB': TeamB, 'TeamC': TeamC}
# create dataframe
df = pd.DataFrame(data)

ax = df.plot(kind='barh', x='labels', width=0.9, figsize=(12, 10), xlabel='',
             color=['#b02a2a', '#055cad', '#0b7d53'], stacked=True)
ax.set_xlabel('Events', fontsize=15)
ax.set_xlim(0, 100)

ax.legend(loc='upper right', frameon=False, fontsize=15, markerscale=2)

for c in ax.containers:
    labels = [f'{w:.0f}' if (w := v.get_width()) > 1 else '' for v in c]
    ax.bar_label(c, labels=labels, label_type='center', padding=1, size=15)
    
ax.tick_params(axis='both', which='both', labelsize=15)
ax.spines[['top', 'right']].set_visible(False)

plt.show()
