3

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." Group Bar Chart

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
  • Are you looking for something like this, or perhaps trying to use the hue variable in the documentation you've referenced? Commented Apr 14, 2016 at 18:17

3 Answers 3

4

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

ggplot grouped bar

Sign up to request clarification or add additional context in comments.

Comments

0

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)

enter image description here

PPS you may also want to check this answer

1 Comment

Almost. The index is composed of two items. I want one item (Beta) to appear alongside the graph as a legend. Currently, the x-label is formatted in the following way ('Alpha', Beta)
-1

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

enter image description here

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.