I have a dataframe where I have a variable 'Gender' (0 or 1) indicating if one is Male or Female, and another variable 'Dis' which says the state of the Disease (0,1,2 or 3).
> df.head()
Gender Dis
0 1 2
1 0 0
2 0 1
3 1 3
4 0 0
5 0 1
I want to make a barplot with the count values for each one of the'Dis' values but I want it to be separated by Gender, i.e, I want two bars for each one of the states of the disease. I want this:
However, I can't do this barplot automatically without manually writing the count values of each one. I had to check the count values for each one of the combinations aside. I produced this plot manually with the following:
X = ['0','1','2','3']
M = [43,9,20,11]
F = [118,21,168,20]
X_axis = np.arange(len(X))
plt.bar(X_axis - 0.2, M, 0.4, label = 'Male')
plt.bar(X_axis + 0.2, F, 0.4, label = 'Female')
plt.xticks(X_axis, X)
plt.xlabel("")
plt.ylabel("")
plt.legend()
plt.title("title")
def autolabel(rects):
for rect in rects:
h = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
ha='center', va='bottom')
plt.show()
Can I do something more "automatic" directly from the dataframe? Also, can I also display the count values on top of each bar?



