python - Plotting a stacked histogram with Pandas with Group By -


i working dataset looks follows:

gender, height, width male, 23.4, 4.4 female, 45.4, 4.5

i'd visualize stacked histograms of height , width. i'm hoping have 2 stacked histograms per plot (one each gender).

this stacked histogram documentation. if there 3 genders, might graph width.

enter image description here

i hope understand mean, i've been slamming head @ hours.

your example pandas documentation has 3 seperate columns in dataframe , df.hist() generates 3 different histograms 3 columns. data structure little different. if you'd use matplotlib directly, can try this:

import numpy np import pandas pd import matplotlib.pyplot plt np.random.seed(10) df = pd.dataframe({"gender":np.random.choice(["female", "male"], 1000),                  "height": 30+np.random.randn(1000)*5,                 "width": 5+np.random.randn(1000)}) df.loc[df["gender"]=="male", "height"] = df.loc[df["gender"]=="male", "height"] + 8  plt.hist(df[df["gender"]=="male"]["height"].reset_index(drop=true), alpha=0.6, label="male") plt.hist(df[df["gender"]=="female"]["height"].reset_index(drop=true), alpha=0.6, label="female") plt.legend() plt.show() 

this create histogram this:

enter image description here


Comments