将Pandas crosstab与Seaborn堆叠式Barplots结合使用

将Pandas crosstab与Seaborn堆叠式Barplots结合使用,第1张

将Pandas crosstab与Seaborn堆叠式Barplots结合使用

如您所说,您可以使用熊猫来创建堆积的条形图。想要拥有“季节性地块”的论点是无关紧要的,因为每个季节性地块和每个熊猫地块最终都只是matplotlib对象,因为两个库的绘图工具都只是matplotlib包装器。

因此,这是一个完整的解决方案(从@andrew_reece的答案中获取数据创建)。

import numpy as np import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltn = 500mark = np.random.choice([True,False], n)periods = np.random.choice(['baseLINE','WEEK 12', 'WEEK 24', 'WEEK 4'], n)df = pd.Dataframe({'mark':mark,'period':periods})ct = pd.crosstab(df.period, df.mark)ct.plot.bar(stacked=True)plt.legend(title='mark')plt.show()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5655386.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存