Skip to content Skip to sidebar Skip to footer

Group Bar Chart With Seaborn/matplotlib

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']).media

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

ggplot grouped bar

Solution 2:

is that what you want?

In [167]: df
Out[167]:
    a  b  c
022113312221323043225332612271228023932310220112121221013121140231503316312170111802219010

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

In [169]: plot
Out[169]:
            c
a b
010.50000022.66666733.000000121.666667211.00000020.66666730.000000312.00000022.50000031.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

Solution 3:

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

Post a Comment for "Group Bar Chart With Seaborn/matplotlib"