[Python] Comparing FacetGrid v Subplot

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I have this Dataframe:

    OverallQual Foundation
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 BrkTil
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 BrkTil
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 Wood
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 CBlock
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    8 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 CBlock
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    9 PConc
    10 BrkTil
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 PConc
    10 BrkTil
    10 PConc
    10 PConc
    10 CBlock
    10 PConc


    And if I count the values of each Foundation in every OverallQual, it show the result like this:

    OverallQual Foundation
    8 BrkTil 2
    CBlock 10
    PConc 155
    Wood 1
    9 CBlock 1
    PConc 42
    10 BrkTil 2
    CBlock 1
    PConc 15
    Name: count, dtype: int64


    I want to produce a faceted grid of bar plots, one for each OverallQual and then colour the bars according to the Foundation column. I try to use FacetGrid in this case. Here's my code

    df_exerc_fg = sns.FacetGrid(df_exerc, col='OverallQual', hue='Foundation',palette='tab10')
    df_exerc_fg.map(sns.barplot, 'Foundation', 'OverallQual')


    But the result is not like what I expected. I think it's not plotting correctly.

    [​IMG]

    But when I compared this with subplot in this code:

    fig,ax = plt.subplots(1, 3, sharey=True, sharex=True, figsize=(15,5))
    plt.suptitle(str.title('Material pondasi yang sering dipakai untuk grade 8-10'), fontsize=16)

    ax[0].set_title('Overall = 8', fontsize=14)
    sns.barplot(x = train_house['Foundation'].loc[train_house.OverallQual == 8].value_counts().index,
    y = train_house['Foundation'].loc[train_house.OverallQual == 8].value_counts(),
    hue = train_house['Foundation'].loc[train_house.OverallQual == 8].value_counts().index,
    legend=False,
    palette='tab10',
    ax=ax[0])

    ax[1].set_title('Overall = 9', fontsize=14)
    sns.barplot(x = train_house['Foundation'].loc[train_house.OverallQual == 9].value_counts().index,
    y = train_house['Foundation'].loc[train_house.OverallQual == 9].value_counts(),
    hue = train_house['Foundation'].loc[train_house.OverallQual == 9].value_counts().index,
    legend=False,
    palette='tab10',
    ax=ax[1])

    ax[2].set_title('Overall = 10', fontsize=14)
    sns.barplot(x = train_house['Foundation'].loc[train_house.OverallQual == 10].value_counts().index,
    y = train_house['Foundation'].loc[train_house.OverallQual == 10].value_counts(),
    hue = train_house['Foundation'].loc[train_house.OverallQual == 10].value_counts().index,
    legend=False,
    palette='tab10',
    ax=ax[2])


    plt.show()


    It plot the data perfectly, like this. [​IMG]

    The chart in FacetGrid is not plotting correctly. What am I missing in my code?

    Continue reading...

Compartilhe esta Página