1

My dataframe looks like this:

Index | A | B | C | D | 
USA   | 1 | 2 | 1 | 2 |
GER   | 2 | 3 | 2 | 3 |
RUS   | 3 | 4 | 3 | 4 |
GBR   | 4 | 5 | 4 | 5 |

(The values are not the true ones) I want to make a bar plot for only the first 3 rows, where each "bar" is a variable (A, B, C, D) and the rows are shown like this example:

enter image description here

Where, for example, USA is blue, GER Is red and RUS Is green. I tried using matplotlib and seaborn, so I'm open to suggestions.

1 Answer 1

1

I believe you need filter first 3 rows by DataFrame.head and then plot all 4 columns A,B,C,D by DataFrame.plot.bar:

df.head(3).plot.bar(stacked=True, colors=['blue','red','green', 'pink'])

Or maybe you want transpose, so:

df.head(3).T.plot.bar(stacked=True, colors=['blue','red','green'])
Sign up to request clarification or add additional context in comments.

2 Comments

What if it's not the first 3 rows? What if the 3 rows I want are not next to each other?
@user12195705 - Then select them by list like df.loc[['USA','RUS','GER']].plot.bar(stacked=True, colors=['blue','red','green','pink'])

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.