4

Say I have the following pandas dataframe:

In[114]: df
Out[114]: 
     0-10%  11-20%  21-30%  31-40%  41-50%  51-60%  61-70%  71-80%  81-90%  \
f    0.186   3.268   3.793   4.554   6.421   6.345   7.383   8.476   8.968   
l    1.752   2.205   2.508   2.866   3.132   3.157   3.724   4.073   4.905   

     91-100%  
f     12.447  
l      8.522

and say I want to produce a barplot where I have the columns as categories on the x axis and, for each category, two bars, one for f and one for l, so to make comparisons.

How to do this in order to avoid the bars being stacked?

My attempt produces stacked bars and an offset in terms of x labels:

x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
labels = ['0-10%','11-20%','21-30%','31-40%','41-50%','51-60%','61-70%','71-80%','81-90%','91-100%']
row1 = df.iloc[0]
row1.plot(kind='bar',title='Binned comparison', color='r',stacked=False)
row2 = df.iloc[1]
row2.plot(kind='bar',title='Binned comparison', color='k',stacked=False)
plt.xticks(x,labels, rotation='horizontal',fontsize=8)

1 Answer 1

3

you can plot.bar on the transpose:

df.T.plot.bar()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! How do you access the colors of the bars and the orientation of the x labels?
@CF84 orientation I don't know off the top of my head. Colors, try: df.T.plot.bar(color=['red', 'aqua'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.