I am looking to change my y axis so that it shows 0, 25, 50, 75, and 100. Additionally, how can I show the data labels over each bar? I tried using ylim but it was not working.
- a - o are just rand ints
group_a = (a,b,c,d,e)
group_b = (f,g,h,i,j)
group_c = (k,l,m,n,o)
width = 0.2
x = np.arange(5)
plt.bar(x-0.2, group_a, width, color = 'cyan')
plt.bar(x, group_b, width, color = 'orange')
plt.bar(x 0.2, group_c, width, color = 'green')
plt.xticks(x, ['1','2','3','4','5'])
plt.xlabel("quarter")
plt.ylabel('%')
plt.legend(['Group A','Group B','Group C'])
plt.show()
CodePudding user response:
Set plt.yticks
to the desired values.
Additionally, how can I show the data labels over each bar?
I'm not really sure if I correctly understand what you want. Take a look below and let me know in the comment.
import numpy as np
import matplotlib.pyplot as plt
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o = np.random.randint(0, 100, 15)
group_a = (a,b,c,d,e)
group_b = (f,g,h,i,j)
group_c = (k,l,m,n,o)
width = 0.2
offset_y = 2
x = np.arange(5)
plt.figure()
plt.bar(x-0.2, group_a, width, color = 'cyan')
plt.bar(x, group_b, width, color = 'orange')
plt.bar(x 0.2, group_c, width, color = 'green')
for _x, t in zip(x, group_a):
plt.text(_x-0.2, t offset_y, str(t), horizontalalignment="center")
for _x, t in zip(x, group_b):
plt.text(_x, t offset_y, str(t), horizontalalignment="center")
for _x, t in zip(x, group_c):
plt.text(_x 0.2, t offset_y, str(t), horizontalalignment="center")
plt.xticks(x, ['1','2','3','4','5'])
plt.xlabel("quarter")
plt.yticks(np.linspace(0, 100, 5, dtype=int))
plt.ylabel('%')
plt.legend(['Group A','Group B','Group C'])
plt.show()