我需要使用Boston Housing Dataset绘制散点图。我想用MEDV列绘制所有其他列。此代码使所有绘图都在同一图形上。如何分隔它们?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)
答案 0 :(得分:2)
如果展平axes
对象,则您的代码将起作用,因为当前您是在axes
(这是一个二维对象)上循环一次。因此,在for循环中使用axes.flatten()
,然后使用ax.scatter
,它将把每一列绘制到一个新图形上。
打印顺序为第一行,然后是第二行,然后是第三行
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)
答案 1 :(得分:1)
您需要使用ax.scatter
而不是plt.scatter
,以便它们在您创建的轴上进行绘制。
答案 2 :(得分:1)
尝试使用ax [row,col] .scatter()进行绘图。这应该可以解决问题。然后,您必须遍历行和列。