Skip to content Skip to sidebar Skip to footer

Histogram Color By Class

I have a pandas dataframe with 2 columns 'height' and 'class, class is a column with 3 values 1,2 and 5. Now i want to make a histogram of the height data and color by class. plot

Solution 1:

Since I'm not sure if the potential duplicate actually answers the question here, this is a way to produce a stacked histogram using numpy.histogram and matplotlib bar plot.

import pandas as pd
import numpy as np;np.random.seed(1)
import matplotlib.pyplot as plt

df = pd.DataFrame({"x" : np.random.exponential(size=100),
                   "class" : np.random.choice([1,2,5],100)})

_, edges = np.histogram(df["x"], bins=10)
histdata = []; labels=[]
for n, group in df.groupby("class"):
    histdata.append(np.histogram(group["x"], bins=edges)[0])
    labels.append(n)

hist = np.array(histdata) 
histcum = np.cumsum(hist,axis=0)

plt.bar(edges[:-1],hist[0,:], width=np.diff(edges)[0],
            label=labels[0], align="edge")

for i inrange(1,len(hist)):
    plt.bar(edges[:-1],hist[i,:], width=np.diff(edges)[0],
            bottom=histcum[i-1,:],label=labels[i], align="edge")

plt.legend(title="class")
plt.show()

enter image description here

Post a Comment for "Histogram Color By Class"