Given the dataset:
timestamp item itemcount
2019-03-18 07:40:08.759 A 10
2019-03-18 08:40:08.759 B 5
..................................................
2019-05-20 07:40:08.759 D 4
2019-05-21 07:40:08.759 E 8
I want to plot stacked histogram like:
where the x-axis should be the date and y axis the itemcount and stack will be each item. I want the graph with subplots for every month.
I am new here so will be happy to get any feedback on my mistakes. Thank you.
here's one sample code i found online which plots the same graph in the figure above.
# Import Data
df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")
# Prepare data
x_var = 'manufacturer'
groupby_var = 'class'
df_agg = df.loc[:, [x_var, groupby_var]].groupby(groupby_var)
vals = [df[x_var].values.tolist() for i, df in df_agg]
# Draw
plt.figure(figsize=(16,9), dpi= 80)
colors = [plt.cm.Spectral(i/float(len(vals)-1)) for i in range(len(vals))]
n, bins, patches = plt.hist(vals, df[x_var].unique().__len__(), stacked=True, density=False, color=colors[:len(vals)])
# Decoration
plt.legend({group:col for group, col in zip(np.unique(df[groupby_var]).tolist(), colors[:len(vals)])})
plt.title(f"Stacked Histogram of ${x_var}$ colored by ${groupby_var}$", fontsize=22)
plt.xlabel(x_var)
plt.ylabel("Frequency")
plt.ylim(0, 40)
plt.xticks(ticks=bins, labels=np.unique(df[x_var]).tolist(), rotation=90, horizontalalignment='left')
plt.show()