Reputation: 67
The value of each bar is not showing at each bar. And, also only one type of bars is showing. Where should I do some adjustments?
import numpy as np
import matplotlib.pyplot as plt
a = [25, 30, 20]
b = [30, 20, 27]
c = [30, 25, 37]
d = [25, 40, 20]
x_ticks = ['Type 1', 'Type 2', 'Type 3']
y = [a, b, c, d]
index = np.arange(3)
plt.xticks(index + .3, x_ticks)
plt.bar(index + 0.00, a, color ='r', width = 0.22)
plt.bar(index + 0.20, b, color ='g', width = 0.22)
plt.bar(index + 0.40, c, color ='b', width = 0.22)
plt.bar(index + 0.60, d, color ='k', width = 0.22)
for a, b in enumerate(y):
plt.text(a, b, str(b))
plt.show()
Upvotes: 0
Views: 76
Reputation: 339052
You need two loops to annotate the bars, one to loop over the types and one to loop over the subgroups.
for i,s in enumerate(y):
for j, t in enumerate(s):
plt.text(j+i*.2, t, str(t), ha="center")
Upvotes: 1