Reputation: 982
Let's say I have data like this:
Column A | Column B |
---|---|
Foo | 5 |
Bar | 46 |
Baz | 100 |
Foo | 10 |
... | ... |
I'm trying to create a stacked histogram from a pandas DataFrame that stacks the series obtained by grouping by Column A the values of Column B.
I used pd.hist(by='Column A', column='Column B', stacked=True)
, but, as the docs say, 'by' will create an histogram by each unique value in the column specified.
Any hints?
Upvotes: 1
Views: 2056
Reputation: 743
You can do this way:
# taking count since histogram anyway represents the frequencies in each bin.
grp = df.groupby("col_A").agg("count")
plt.hist([grp.index, grp.values.reshape(3,)], bins=3, range=(1,6), stacked=True, color = ['r','g'])
plt.show()
Upvotes: 0
Reputation: 23146
IIUC, you want this:
df.pivot(columns="Column A")["Column B"].plot(kind="hist", stacked=True)
Upvotes: 4