来自pandas数据帧的多个条形图

时间:2016-06-21 22:54:33

标签: python pandas matplotlib

所以我有一个像

这样的数据框
exp_name, index, items, clicks
"foo",0, "apple",200
"foo",0, "banana", 300
"foo",0,"melon",220
"foo",1, "apple", 10
"foo",1,"peach", 20
"bar",0, "apple",400
"bar",0,'banana', 500
"bar",0, "melon",240
"bar",1,"apple",500

等等

我想为每个实验名称绘制... ...每个索引中每个项目的点击次数的条形图,但是按索引着色。 基本上...... 情节1 ..对于实验“foo”,条形图..其中index == 0 ..所有条形图的索引0在一种颜色中..索引1在另一种颜色。

如果项目丢失(例如桃子在“foo”,1但不在任何其他地方)在其他地方将“桃子”替换为零。

1 个答案:

答案 0 :(得分:3)

我将您的数据复制/粘贴到名为' test.txt'的txt文件中。并重命名" index" as" status"避免与DataFrame索引混淆。然后我使用Seaborn库来制作条形图,其中包含你提到的偶然事件(据我所知)。我使用子图而不是使用颜色来区分" status"因为我个人认为它看起来更干净,但我使用下面的颜色,因为那是你所问的。

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

df = pd.read_csv('test.txt')  
fig, ax = sns.plt.subplots(1, 1, figsize=(7,5))
sns.factorplot(x="items", y="clicks", hue="exp_name", col="status", data=df, kind="bar")
plt.show()

提供以下内容:enter image description here

如果你真的想区分" index" (我称之为" status")按颜色,您可以定义一个新变量,它结合了" exp_name"与"状态"

df['exp'] = df.exp_name + df.status.astype(str)
sns.factorplot(x="items", y="clicks", hue="exp", data=df, kind="bar")

给出类似这样的东西

enter image description here

如果您有更多问题,请查看seaborn的文档。它是一个非常棒的分类数据库。更改图例标签和其他设置遵循matplotlib约定。