我正在尝试将线性回归图和残差图一个接一个地可视化-甚至可能吗?我知道如何分别绘制它们,但是有人可以帮助我在同一张画布上绘制它们吗?这是我的尝试,但是绘制了第一个图,而第二个图则留空。我收到错误消息:'AxesSubplot'对象没有属性'sns'
fig1, ax = plt.subplots(1, 2, figsize=(12, 3))
ax[0].scatter(X_train, y_train, color = 'red')
ax[0].plot(X_train, lm1.predict(X_train), color = 'blue')
ax[0].set_title('Simple Linear Regression')
ax[0].set_xlabel('highway-mpg')
ax[0].set_ylabel('price')
ax[1].sns.residplot(df['highway-mpg'], df['price'])
ax[1].set_title('Residual plot')
ax[1].set_xlabel('highway-mpg')
ax[1].set_ylabel('price')
fig1
答案 0 :(得分:1)
应该是:
fig1, ax = plt.subplots(1, 2, figsize=(12, 3))
ax[0].scatter(X_train, y_train, color = 'red')
ax[0].plot(X_train, lm1.predict(X_train), color = 'blue')
ax[0].set_title('Simple Linear Regression')
ax[0].set_xlabel('highway-mpg')
ax[0].set_ylabel('price')
### change is here
sns.residplot(df['highway-mpg'], df['price'], ax=ax[1])
ax[1].set_title('Residual plot')
ax[1].set_xlabel('highway-mpg')
ax[1].set_ylabel('price')