Group Bar Chart with Seaborn/Matplotlib

为君一笑 提交于 2019-12-24 00:35:27

问题


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()

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

import seaborn as sns
sns.barplot(x=['Alpha', 'Beta'], y=xl2['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.

Thanks for your help!


回答1:


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()




回答2:


is that what you want?

In [167]: df
Out[167]:
    a  b  c
0   2  2  1
1   3  3  1
2   2  2  1
3   2  3  0
4   3  2  2
5   3  3  2
6   1  2  2
7   1  2  2
8   0  2  3
9   3  2  3
10  2  2  0
11  2  1  2
12  2  1  0
13  1  2  1
14  0  2  3
15  0  3  3
16  3  1  2
17  0  1  1
18  0  2  2
19  0  1  0

In [168]: plot = df.groupby(['a','b']).mean()

In [169]: plot
Out[169]:
            c
a b
0 1  0.500000
  2  2.666667
  3  3.000000
1 2  1.666667
2 1  1.000000
  2  0.666667
  3  0.000000
3 1  2.000000
  2  2.500000
  3  1.500000

In [170]: sns.barplot(x=plot.index, y=plot.c)

PS if you need something different, please provide a sample data set and expected grouped resulting DF (both in text/dict/JSON/CSV form)

PPS you may also want to check this answer




回答3:


Altair can be helpful in such cases. Here is the plot produced by the following code.

Imports

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

Generating dataset

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

Plotting

Chart(df).mark_bar().encode(x=X('cat', axis=False),  
                            y=Y('median(w)', axis=Axis(grid=False)),
                            color='cat',
                            column=Column('x', axis=Axis(axisWidth=1.0, offset=-8.0, orient='bottom'),scale=Scale(padding=30.0)),
                        ).configure_facet_cell( strokeWidth=0.0).configure_cell(width=200, height=200)

The key things in the altair code are:

  1. X values are categories ('cat' in the df)
  2. Color is by category
  3. Y values are by median of the variable
  4. Different columns represent different years


来源:https://stackoverflow.com/questions/36630771/group-bar-chart-with-seaborn-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!