0

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.

1
  • The first approach with a single call to pairplot seems desireable; unless you have a reason not to use it. I do not understand that reason. If you mind explaining it a bit more in detail, it would be easier to help. Also, having a runnable example code available helps giving answers. Commented Nov 23, 2018 at 13:14

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.