2

我的代码如下:

import seaborn as sns
from itertools import product

titanic = sns.load_dataset("titanic")

sex_order = titanic['sex'].unique().tolist() 
hue_order = titanic['survived'].unique().tolist()
bar_order = product(sex_order, hue_order)

catp = sns.catplot(data=titanic, kind='count', 
                   x='sex', hue='survived',
                   order = sex_order, 
                   hue_order = hue_order )


spots = zip(catp.ax.patches, bar_order)
for spot in spots:
    total = len(titanic[(titanic['sex']==spot[1][0]) & 
        (titanic['survived']==spot[1][1])])
    height = spot[0].get_height() 
    catp.ax.text(spot[0].get_x()+0.125, height+3, '{:1.0f}'.format(total))

输出图表如下:

猫图

现在我的问题是为什么某些条形图的计数显示错误。例如,对于红色箭头和黄色箭头。他们的号码相互交换。

我该如何解决这个问题?

4

0 回答 0