我正在Kaggle上制作一个广受欢迎的泰坦尼克号数据集,我想制作一个条形图,显示幸存者和死者的性别数字。在x轴上,我想要性别(男/女)。我想把生还者和死者放在一起,并用彩色编码
这是我当前的代码,它为男性/幸存、男性/死亡、女性/幸存、女性/死亡的每个组合生成四个条:
import pandas as pd
import seaborn as sns # for the data
df = sns.load_dataset('titanic').loc[:, ['sex', 'survived']]
df.groupby('sex').survived.value_counts().plot(kind='bar', color=['C0', 'C1'], stacked=True)
对于这种精细的绘图,尤其是数据帧,我更喜欢使用plotly,因为输出更具交互性。我没有使用组操作,因为逻辑索引可以实现这一点
最后,由于堆叠直方图的性质,您只能表示死者,因为幸存者将是左侧区域。如果你想用不同的颜色来表达它们,请随时发表评论
希望它能解决你的问题
kind='bar'
和stacked=True
进行绘图。pandas v1.2.4
和matplotlib v3.3.4
(matplotlib
由pandas
作为依赖项导入)李>对于一些示例数据,我相信这就是您正在寻找的,使用
matplotlib
:这是输出:
相关问题 更多 >
编程相关推荐