I am trying to plot a histogram of multiple attributes grouped by another attributes, all of them in a dataframe.
with the help of this question, I am able to set title
You can almost get what you want by doing:
g.plot(kind='bar')
but it produces one plot per group (and doesn't name the plots after the groups so it's a bit useless IMO.)
Here's something which looks rather beautiful, but does involve quite a lot of "manual" matplotlib
work, which everyone wants to avoid, but no one can:
import numpy.random as rnd
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
x = pd.DataFrame(rnd.randn(100).reshape(20, 5), columns=list('abcde'))
group_col = 'groups'
groups = ['foo', 'bar', 'baz']
x[group_col] = pd.Series(rnd.choice(groups, len(x)))
g = x.groupby(group_col)
num_groups = g.ngroups
fig, axes = plt.subplots(num_groups)
for i, (k, group) in enumerate(g):
ax = axes[i]
ax.set_title(k)
group = group[[c for c in group.columns if c != group_col]]
num_columns = len(group.columns)
colours = cm.Spectral([float(x) / num_columns for x in range(num_columns)])
ax.hist(group.values, 5, histtype='bar',
label=list(group.columns), color=colours,
linewidth=1, edgecolor='white')
ax.legend()
plt.show()
Which I think gives you what you want:
groupby
objects but I don't know of it.
Here's the simplest possible way to do this:
axes = g.plot(kind='hist')
for i, (groupname, group) in enumerate(g):
axes[i].set_title(groupname)