python stacked bar chart using categorical data


I have a Pandas dataframe (1800 obs) that looks something like this:

      A      B      C      D
 1   CL0    CL1    CL2    CL0
 2   CL2    CL1    CL1    CL3
 3   CL3    CL2    CL0    CL1
 .   ...    ...    ...    ...
 .   ...    ...    ...    ...
 n   CL2    CL1    CL0    CL3

I want to create a stacked bar chart that will have columns 'A', 'B', 'C', 'D' on the x axis, and the percentage of each level in that feature on the y axis. Something like the picture below.

I'm guessing I need to tabulate the data somehow? But I don't know how to do this.

enter image description here


Answers:


print(df)

Output:

     A    B    C    D
1  CL0  CL1  CL2  CL0
2  CL2  CL1  CL1  CL3
3  CL3  CL2  CL0  CL1

Using .apply()

counts = df.apply(lambda x: x.value_counts() / len(x)).transpose()
fig = plt.figure()
ax = fig.add_subplot(111)
counts.plot(ax=ax,kind='bar', stacked=True, rot=0)
vals = ax.get_yticks()
ax.set_yticklabels(['{:3.2f}%'.format(x*100) for x in vals])
ax.yaxis.grid(True)
ax.set_axisbelow(True)
plt.show()

Output:

enter image description here