I have the following dataframe:
df = pd.DataFrame(data = {'id' : ['hbo', 'hbo', 'hbo', 'fox','fox','fox', 'NBC', 'NBC', 'NBC'],
'device': ['laptop', 'tv', 'cellphone','laptop', 'tv', 'cellphone','laptop', 'tv', 'cellphone'],
'count': [100,200,300, 400, 300, 200, 900, 1000, 100],
'date': ['2022-04-30', '2022-05-30','2022-06-30', '2022-07-30', '2022-08-30','2022-09-30',
'2022-10-30', '2022-11-30','2022-12-30']})
I am trying to code and make a stacked bar chart, with the following:
- Bar Chart is stacked by count, so like a normal stacked bar chart
- Annotation on the stacked bar chart areas showing the % (percentage)
- x-axis is to show the date
- y-axis to show count
- color is defined by device - e.g. tv = 'red'
The stacked bar chart is over time (as oppposed to line chart)
So far I have the following code, but am stuck on how to pivot the data and create the output using matplotlib:
fig, ax = plt.subplots(1,1)
ax = df.plot.bar(stacked=True, figsize=(10, 6), ylabel='count', xlabel='dates', title='count of viewing type over time', ax = ax)
plt.legend(title='Category', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.xlabel('Date', fontsize = 14)
plt.ylabel('Count of viewing type', fontsize = 14)
plt.title('plot test', fontsize = 20)
plt.xticks(rotation = 45)
plt.show();