Python 绘图:具有三列的堆栈栏
我有下面的数据框Python 绘图:具有三列的堆栈栏,python,pandas,charts,bar-chart,Python,Pandas,Charts,Bar Chart,我有下面的数据框 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 ''' 我使用下面的代码图上面的数据 import matplotlib.ticker as mtick import matplotlib.pyplot as plt df.groupby(['gender','type']).cou
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
'''
我使用下面的代码图上面的数据
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()
我的输出:
问:我想用数据的时间来绘制这两条线。
如果有人能帮忙,我将不胜感激 使用
matplotlib
,然后
(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)
)
输出:
涉及数据时间的
是什么意思?是否需要y
轴上的实际时间?是。我想用实际时间画条谢谢你的回答。如何缩放y轴100%?什么意思缩放y轴100
?那不再是你的实际时间了,是吗?实际时间。例:F只有A型和B型。但我想用100度来表示实际时间的比率。我很抱歉我的解释啊,我明白了。更新。谢谢!这是我想要的,但却无法实现。