I have the following dataset that contains weekly information for 4 different levels. I created a stacked bar chart, each stack representing a level, and each bar a week. How can I add the values corresponding to each color on chart?
import numpy as np
import pandas as pd
import matplotlib as plt
plt.style.use('ggplot')
%matplotlib inline
rng=pd.date_range('2020-01-02', periods=10,freq='7D')
level=['low', 'medium', 'high', 'very_high']
values=np.random.randint(1, 100, size=(10, 4))
df=pd.DataFrame(index=rng, data=values, columns=level)
df.plot.bar(stacked=True,figsize=(15,10), alpha=0.6)
plt.legend(loc='upper right', bbox_to_anchor=(1.1, 1.05))
I tried using a similar question using this code, but it does not seem to work for 4 axis. Also, all the bars should be equal to 1 and they aren't
res = df.div(df.sum(axis=1), axis=0)
fig = plt.figure(facecolor="white")
ax = fig.add_subplot(1, 1, 1)
bar_width = 3
bar_l = df.index
# tick_pos = [i + (bar_width / 2) for i in bar_l]
ax1 = ax.bar(bar_l, res['low'], width=bar_width, label="A", color="green")
ax2 = ax.bar(bar_l, res['medium'], bottom=res['low'], width=bar_width, label="medium", color="blue")
ax3 = ax.bar(bar_l, res['high'], bottom=res['low'], width=bar_width, label="high", color="yellow")
ax4 = ax.bar(bar_l, res['very_high'], bottom=res['low'], width=bar_width, label="very_high", color="red")