我一直在尝试在条形图和直方图的上方添加标签,但是网上似乎没有办法解决我的问题。我在做错什么吗?
以下是我的数据:
y outcome
education
basic.4y no 3748
basic.4y yes 428
basic.6y no 2104
basic.6y yes 188
basic.9y no 5572
basic.9y yes 473
high.school no 8484
high.school yes 1031
illiterate no 14
illiterate yes 4
professional.course no 4648
professional.course yes 595
university.degree no 10498
university.degree yes 1670
unknown no 1480
unknown yes 251
def get_bar(df,attr):
plt.rcParams["figure.figsize"] = (9,9)
in_length = len(df.index.unique())
yes = df[df.y == 'yes']
no = df[df.y == 'no']
p1 = plt.bar(np.arange(1,in_length+1),yes.outcome,width = 0.35,color = '#008CC5')
p2 = plt.bar(np.arange(1,in_length+1),no.outcome,width = 0.35,bottom = yes.outcome,color = '#FC8E33')
plt.ylabel('people')
plt.xticks(np.arange(1,in_length+1),df.index.unique(),rotation = 45,ha = 'right')
plt.title("Client subscription of a term deposit based on %s"%(attr))
plt.legend((p1[0],p2[0]),('Yes','No'))
return (plt)
上面的代码生成了该图,但是我需要在每个条形上方的标签上加上收集数量。
我应该怎么做?
同时,如何在以下代码中将其添加到直方图中??
def get_hist(attr):
plt.rcParams["figure.figsize"] = (12,12)
yes = df[df.y == 'yes'][attr]
no = df[df.y == 'no'][attr]
color = ['#008CC5','#FC8E33']
plt.hist([no,yes],rwidth=0.5 ,color=color)
return plt
有没有解决此问题的“熊猫”方法?