I want to create a stacked histogram like below.
Here is my code:
import numpy as np
import pandas as pd
import datetime
import matplotlib.pyplot as plt
def stackhist(x, y):
grouped = pd.groupby(x, y)
data = [d for _, d in grouped]
labels = [l for l, _ in grouped]
plt.figure(figsize=(20, 10))
plt.hist(data, histtype="bar", stacked="True", label=labels)
plt.legend()
# make data distribution
mu, sigma = 12.2, 1.2
distribution = np.random.normal(mu, sigma, 200)
times = [(datetime.time(hour=int(x), minute=int((x - int(x))*60.0), second=int(((x - int(x)) * 60 - int((x - int(x))*60.0))*60.0))).strftime('%H:%M:%S') for x in distribution]
df = pd.DataFrame(columns=['time', 'department'])
df.time = times
df['department'] = df['department'].fillna(pd.Series(np.random.choice(['Shoes', 'Hats', 'Shirts', 'Pants'],
p=[0.1, 0.15, 0.375, 0.375], size=len(df))))
stackhist(df['time'], df['department'])
plt.show()
Here is the output notice that the X label is all the different times stacked. How can I make it just be the hours as in 10-11-12-13-14-15-16 and not the minutes:
thank you for your attention.