2
votes

I have a datafarme and would like to create multiple bars chart.

Basically, I created the table below after I ran the code:

company = df.groupby('country').status.value_counts()
company
country       status   
DNK           operating    186
              acquired      13
              closed        10
FIN           operating    171
              acquired      11
              closed         8
ISL           operating     14
              closed         2
NOR           operating     85
              acquired       6
              closed         6
SWE           operating    277
              closed        18
              acquired      12

I tried to create multiple bars chart that each country is grouping 3 status values. Each status is representing a bar color (operating = blue, acquired = green, closed = red). However, I get either error or all of them has one color. The chart should be same as the image sample below:

image sample for multiple bars chart

I'm new and hope you can help me with this.

2

2 Answers

1
votes

You could use seaborn:

import seaborn as sns
sns.set()

sns.catplot(x="country", y="value", hue="status", kind="bar", data=company)

Here, value would be the column name of your numerical column. And you may have to fill up the country column first of all, so that your DataFrame looks like this:

country       status       value
DNK           operating    186
DNK           acquired      13
DNK           closed        10
FIN           operating    171
FIN           acquired      11
FIN           closed         8
ISL           operating     14
ISL           closed         2
NOR           operating     85
NOR           acquired       6
NOR           closed         6
SWE           operating    277
SWE           closed        18
SWE           acquired      12

Then it should work:

barplot

0
votes

Based on @Arne final output, basically I need to assign value for the output of status.value_counts() to get my chart without any error by adding reset_index(name='value') to the code.

company = df.groupby('country').status.value_counts().reset_index(name='value')

company
    country    status       counts
0   DNK        operating    186
1   DNK        acquired     13
2   DNK        closed       10
3   FIN        operating    171
4   FIN        acquired     11
5   FIN        closed       8
6   ISL        operating    14
7   ISL        closed       2
8   NOR        operating    85
9   NOR        acquired     6
10  NOR        closed       6
11  SWE        operating    277
12  SWE        closed       18
13  SWE        acquired     12

Then I continue with the same code that @Arne suggested:

import seaborn as sns
sns.set()

sns.catplot(x="country", y="value", hue="status", kind="bar", data=company)

The output came out same as @Arne's chart.

Thank you @Arne!