Reputation: 692
I have below dataframe.
df = '''type time gender
0 A 660.0 M
1 B 445.0 M
2 C 68.0 M
3 A 192.2 F
4 B 82.9 F
'''
I use the below code plot above data.
import matplotlib.ticker as mtick
import matplotlib.pyplot as plt
df.groupby(['gender','type']).count().groupby(level=0).apply(
lambda x:100*x / x.sum()
).unstack().plot(kind='bar',stacked=True)
plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter())
plt.legend( prop={'size': 16})
plt.show()
My output:
Q: I want to plot these two bars with involving the time of the data. Appreciate it if someone can help!
Upvotes: 1
Views: 102
Reputation: 150745
Using matplotlib
, then
(df.pivot_table(index='gender', columns='type',
values='time', aggfunc='sum',
fill_value=0)
.apply(lambda x: x/x.sum(), axis=1)
.plot.bar(stacked=True)
)
Output:
Upvotes: 1