带有Seaborn/Matplotlib的组条形图 [英] Group Bar Chart with Seaborn/Matplotlib

查看:112
本文介绍了带有Seaborn/Matplotlib的组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用由两个变量"Alpha"和"Beta"分组的pandas DataFrame来创建如下所示的分组条形图.

My goal is to create a grouped bar chart like the one below, using a pandas DataFrame that is grouped by two variables "Alpha" and "Beta."

xl2 = xl.groupby(['Alpha','Beta']).median()

当我尝试此操作时,在'Alpha'上抛出了KeyError

When I tried this, a KeyError was thrown on 'Alpha'

import seaborn as sns
sns.barplot(x=['Alpha', 'Beta'], y=xl2['Gamma'])

我的希望是传递一个x值列表以在("Alpha"和"Beta")上建立索引,并绘制相关的"Gamma".

My hope was to pass in a list of x values to index on ('Alpha' and 'Beta'), and graph the associated 'Gamma." The documentation for the seaborn.barplot function doesn't provide any group bar chart examples.

感谢您的帮助!

推荐答案

您可以为此使用ggplot

You can use ggplot for this

from ggplot import *
import pandas as pd
import numpy as np

df = pd.DataFrame({
    "x": np.random.choice(range(2001, 2008), 250),
    "w": np.random.uniform(50, 400, 250),
    "cat": np.random.choice(["A", "B", "C", "D", "E"], 250)
})

print ggplot(df, aes(x='x', weight='w', fill='cat')) + geom_bar() + theme_bw()

这篇关于带有Seaborn/Matplotlib的组条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆