簡體   English   中英

如何在 Facetgrid 中一起繪制 20 個不同的散點圖 - Python(seaborn)

[英]how to plot 20 different scatter subplots together in a Facetgrid - Python (seaborn)

我在 facetgrid 中看到了多個其他關於 seaborn 子圖的線程,但沒有針對我的特定情況。

我有 20 列住房數據。 我想使用 seaborn 在 5 行 x 4 矩陣 FacetGrid 中繪制每個變量與變量“SalePrice”的關系。

這是我選擇的列列表,並且有一個名為“train_df”的當前數據框。

train_cols_to_keep = ['1stFlrSF', '2ndFlrSF', 'Fireplaces', 'FullBath', 'GarageArea', 'GarageCars', 
'GarageYrBlt', 'GrLivArea', 'HalfBath', 'Id', 'LotArea', 'LotFrontage', 'MasVnrArea', 'OpenPorchSF', 
'OverallQual', 'TotalBsmtSF', 'TotRmsAbvGrd', 'WoodDeckSF', 'YearBuilt', 'YearRemodAdd', 
'SalePrice']

train_data_reduced = train_data[train_cols_to_keep]

train_df = train_data_reduced.fillna(train_data_reduced.mean())

我真的不知道如何做到這一點,我看到的每個例子都不包括繪制每一列與其中一列的問題。 謝謝。 同樣可以接受的是將 rpy2 庫用於 ggplots

我認為沒有辦法獲得您嘗試使用seaborn獲得的確切結果。 你能得到的最接近的是使用pairplot()但你會得到一行 20 列軸

g = sns.pairplot(data=df,
                 x_vars=['1stFlrSF', '2ndFlrSF', 'Fireplaces', 'FullBath', 'GarageArea', 'GarageCars', 
'GarageYrBlt', 'GrLivArea', 'HalfBath', 'Id', 'LotArea', 'LotFrontage', 'MasVnrArea', 'OpenPorchSF', 
'OverallQual', 'TotalBsmtSF', 'TotRmsAbvGrd', 'WoodDeckSF', 'YearBuilt', 'YearRemodAdd'],
                 y_vars=['SalePrice'])

如果你直接使用 matplotlib,你可以毫不費力地得到想要的結果

fig, axs = plt.subplots(4,5, sharey=True)
for ax,col in zip(axs.flat, ['1stFlrSF', '2ndFlrSF', 'Fireplaces', 'FullBath', 'GarageArea', 'GarageCars', 
'GarageYrBlt', 'GrLivArea', 'HalfBath', 'Id', 'LotArea', 'LotFrontage', 'MasVnrArea', 'OpenPorchSF', 
'OverallQual', 'TotalBsmtSF', 'TotRmsAbvGrd', 'WoodDeckSF', 'YearBuilt', 'YearRemodAdd']):
    ax.scatter(df[col],df['SalePrice'])
    ax.set_xlabel(col)
    if ax.is_first_col():
        ax.set_ylabel('SalePrice')
fig.tight_layout()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM