简体   繁体   中英

Plot subplots using seaborn pairplot

If I draw the plot using the following code, it works and I can see all the subplots in a single row. I can specifically break the number of cols into three or two and show them. But I have 30 columns and I wanted to use a loop mechanism so that they are plotted in a grid of say 4x4 sub-plots

regressionCols = ['col_a', 'col_b', 'col_c', 'col_d', 'col_e']
sns.pairplot(numerical_df, x_vars=regressionCols, y_vars='price',height=4, aspect=1, kind='scatter')
plt.show() 

The code using loop is below. However, I don't see anything rendered.

 nr_rows = 4
 nr_cols = 4

 li_cat_cols = list(regressionCols)
 fig, axs = plt.subplots(nr_rows, nr_cols, figsize=(nr_cols*4,nr_rows*4), squeeze=False)

 for r in range(0, nr_rows):
      for c in range(0,nr_cols):  
         i = r*nr_cols+c

         if i < len(li_cat_cols):
             sns.set(style="darkgrid")
             bp=sns.pairplot(numerical_df, x_vars=li_cat_cols[i], y_vars='price',height=4, aspect=1, kind='scatter')
             bp.set(xlabel=li_cat_cols[i], ylabel='Price')
 plt.tight_layout()    
 plt.show()

Not sure what I am missing.

I think you didnt connect each of your subplot spaces in a matrix plot to scatter plots generated in a loop.

Maybe this solution with inner pandas plots could be proper for you: For example,

1.Lets simply define an empty pandas dataframe.

numerical_df = pd.DataFrame([])

2. Create some random features and price depending on them:

numerical_df['A'] = np.random.randn(100)
numerical_df['B'] = np.random.randn(100)*10 
numerical_df['C'] = np.random.randn(100)*-10
numerical_df['D'] = np.random.randn(100)*2
numerical_df['E'] = 20*(np.random.randn(100)**2)
numerical_df['F'] = np.random.randn(100)
numerical_df['price'] = 2*numerical_df['A'] +0.5*numerical_df['B'] - 9*numerical_df['C'] + numerical_df['E'] + numerical_df['D']

3. Define number of rows and columns. Create a subplots space with nr_rows and nr_cols.

nr_rows = 2 
nr_cols = 4
fig, axes = plt.subplots(nrows=nr_rows, ncols=nr_cols, figsize=(15, 8))
for idx, feature in enumerate(numerical_df.columns[:-1]):
    numerical_df.plot(feature, "price", subplots=True,kind="scatter",ax=axes[idx // 4,idx % 4])

4. Enumerate each feature in dataframe and plot a scatterplot with price:

for idx, feature in enumerate(numerical_df.columns[:-1]):

    numerical_df.plot(feature, "price", subplots=True,kind="scatter",ax=axes[idx // 4,idx % 4])

where axes[idx // 4, idx % 4] defines the location of each scatterplot in a matrix you create in (3.)

So, we got a matrix plot:

Scatterplot matrix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM