Srirekha Srirekha
Srirekha Srirekha

Reputation: 83

creating bar chart with different groups in different colors in python

I'm trying to create python bar chart with different groups in different colors and all individual rectangles in a specific group should be in same color. I've my data in python pandas dataframe. my dataframe df looks like this

GroupName   ID          Values
Group1      3           39.357895
Group1      12          24.747664
Group1      18          33.721429
Group1      90          37.064516
Group2      20          22.100629
Group2      26          21.821429
Group2      68          23.396552
Group3       1           13.623239
Group3       38          14.312950
Group3       33          16.161616

I want my desired output like this enter image description here

Upvotes: 2

Views: 2805

Answers (1)

Simon
Simon

Reputation: 10150

Using seaborn library you can do something like this:

import seaborn
import pandas as pd

csvfile = "C:/Users/Simon/Desktop/test.csv"

data = pd.read_csv(csvfile)

fg = seaborn.factorplot(x='ID', y='Values', hue='GroupName', kind='bar', data=data)

That will get you something like this:

enter image description here

In my example data is a dataframe created by reading a csv file, but this will work regardless of how you got your data in, as long as its a dataframe

Upvotes: 4

Related Questions