您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

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

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

import numpy as np 
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

n = 500
mark = 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()
其他 2022/1/1 18:38:46 有348人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶