在python中创建具有不同颜色的不同组的条形图
问题描述:
我正在尝试创建具有不同颜色的不同组的python条形图,并且特定组中的所有单个矩形都应使用相同的颜色.我将数据存储在python pandas dataframe中. 我的数据框df看起来像这样
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
答
使用seaborn库,您可以执行以下操作:
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)
那会给你这样的东西:
在我的示例中,数据是通过读取csv文件创建的数据帧,但是无论您如何获取数据,只要它是数据帧,此数据帧都将起作用
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