3

I need to make scatter plots using Boston Housing Dataset. I want to plot all the other columns with MEDV column. This code makes all plot on the same graph. How can I separate them?enter image description here

import matplotlib.pyplot as plt
%matplotlib inline
fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(12, 12))
for column, ax in zip(['CRIM', 'ZN','INDUS', 'CHAS', 'NOX', 'RM'], axes):
    plt.scatter(boston_df[column], boston_df.MEDV)

3 Answers 3

2

Your code will work if you flatten the axes object because currently you are looping once over axes which is a 2-d object. So use axes.flatten() in the for loop and then use ax.scatter which will plot each column to a new figure.

The order of plotting will be the first row, then the second row and then the third row

fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(12, 12))
for column, ax in zip(['CRIM', 'ZN','INDUS', 'CHAS', 'NOX', 'RM'], axes.flatten()):
    ax.scatter(boston_df[column], boston_df.MEDV)
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use ax.scatter instead of plt.scatter so that they plot in the axes you've created.

Comments

1

Try plotting with ax[row,col].scatter(). This should do the trick. You have to iterate over both, rows and columns then.

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.