Reputation: 35
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. plot19_s["vegetation height"].plot.hist(bins = 10)
this is my histogram
but now I want to see the different classes by a change in color in the histogram.
Upvotes: 2
Views: 3986
Reputation: 339340
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 in range(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()
Upvotes: 1