Reputation: 63
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)
Upvotes: 3
Views: 252
Reputation: 39052
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)
Upvotes: 2
Reputation: 166
Try plotting with ax[row,col].scatter(). This should do the trick. You have to iterate over both, rows and columns then.
Upvotes: 1
Reputation: 888
You need to use ax.scatter
instead of plt.scatter
so that they plot in the axes you've created.
Upvotes: 1