第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何向 matplotlib/seaborn 條形圖添加第二個(gè)軸并使輔助點(diǎn)與正確的條形對(duì)齊?

如何向 matplotlib/seaborn 條形圖添加第二個(gè)軸并使輔助點(diǎn)與正確的條形對(duì)齊?

明月笑刀無(wú)情 2023-08-22 10:48:35
我寫了一個(gè)(新手)python 函數(shù)(如下)來(lái)繪制一個(gè)按主要維度和可能的次要維度劃分的條形圖。例如,下圖顯示了每種性別中獲得特定教育水平的人數(shù)百分比。問(wèn)題:如何在每個(gè)條形上疊加該子組的家庭規(guī)模中位數(shù),例如在大學(xué)/女性條形上放置一個(gè)表示值“3”的點(diǎn)。我見(jiàn)過(guò)的例子都沒(méi)有準(zhǔn)確地將點(diǎn)覆蓋在正確的欄上。我對(duì)此非常陌生,所以非常感謝您的幫助!df = pd.DataFrame({'Student'       : ['Alice', 'Bob', 'Chris',  'Dave',    'Edna',    'Frank'],                    'Education'     : ['HS',    'HS',  'HS',     'College', 'College', 'HS'   ],                   'Household Size': [4,        4,     3,        3,         3,         6     ],                   'Gender'        : ['F',     'M',   'M',      'M',       'F',       'M'    ]});def MakePercentageFrequencyTable(dataFrame, primaryDimension, secondaryDimension=None, extraAggregatedField=None):    lod = dataFrame.groupby([secondaryDimension]) if secondaryDimension is not None else dataFrame    primaryDimensionPercent = lod[primaryDimension].value_counts(normalize=True) \                         .rename('percentage') \                         .mul(100) \                         .reset_index(drop=False);    if secondaryDimension is not None:        primaryDimensionPercent = primaryDimensionPercent.sort_values(secondaryDimension)        g = sns.catplot(x="percentage", y=secondaryDimension, hue=primaryDimension, kind='bar', data=primaryDimensionPercent)    else:        sns.catplot(x="percentage", y='index', kind='bar', data=primaryDimensionPercent)        MakePercentageFrequencyTable(dataFrame=df,primaryDimension='Education', secondaryDimension='Gender')# Question: I want to send in extraAggregatedField='Household Size' when I call the function such that # it creates a secondary 'Household Size' axis at the top of the figure# and aggregates/integrates the 'Household Size' column such that the following points are plotted# against the secondary axis and positioned over the given bars:## Female/College => 3# Female/High School => 4# Male/College => 3# Male/High School => 4到目前為止我所取得的成就的圖片
查看完整描述

1 回答

?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊

您將必須使用軸級(jí)函數(shù)sns.barplot()和sns.stripplot()而不是catplot(),它會(huì)創(chuàng)建一個(gè)新圖形和FacetGrid。


像這樣的東西:


df = pd.DataFrame({'Student'       : ['Alice', 'Bob', 'Chris',  'Dave',    'Edna',    'Frank'], 

                   'Education'     : ['HS',    'HS',  'HS',     'College', 'College', 'HS'   ],

                   'Household Size': [4,        4,     3,        3,         3,         6     ],

                   'Gender'        : ['F',     'M',   'M',      'M',       'F',       'M'    ]});



def MakePercentageFrequencyTable(dataFrame, primaryDimension, secondaryDimension=None, extraAggregatedField=None, ax=None):

    ax = plt.gca() if ax is None else ax

    lod = dataFrame.groupby([secondaryDimension]) if secondaryDimension is not None else dataFrame


    primaryDimensionPercent = lod[primaryDimension].value_counts(normalize=True) \

                         .rename('percentage') \

                         .mul(100) \

                         .reset_index(drop=False);


    if secondaryDimension is not None:

        primaryDimensionPercent = primaryDimensionPercent.sort_values(secondaryDimension)

        ax = sns.barplot(x="percentage", y=secondaryDimension, hue=primaryDimension, data=primaryDimensionPercent, ax=ax)

    else:

        ax = sns.barplot(x="percentage", y='index', data=primaryDimensionPercent, ax=ax)

    

    if extraAggregatedField is not None:

        ax2 = ax.twiny()

        extraDimension = dataFrame.groupby([primaryDimension, secondaryDimension]).mean().reset_index(drop=False)

        ax2 = sns.stripplot(data=extraDimension, x=extraAggregatedField, y=secondaryDimension, hue=primaryDimension, 

                            ax=ax2,dodge=True, edgecolors='k', linewidth=1, size=10)



plt.figure()

MakePercentageFrequencyTable(dataFrame=df,primaryDimension='Education', secondaryDimension='Gender', extraAggregatedField='Household Size')

https://img1.sycdn.imooc.com//64e422550001ac5f06410477.jpg

查看完整回答
反對(duì) 回復(fù) 2023-08-22
  • 1 回答
  • 0 關(guān)注
  • 3920 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)