Matplotlib subplots
возвращает массив объектов Axes (если вы запрашиваете несколько вспомогательных участков). Используйте эти объекты Axes, чтобы определить, где должны быть построены данные.
Вот пример, основанный на вашем коде:
import matplotlib.pyplot as plt
import pandas as pd
y_test=[3,4,5]
y_pred=[6,7,8]
df = pd.DataFrame(
{"Actual value of medv": y_test, "Predicted": y_pred}
)
df1 = df.head()
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
# plot the Bar graph showing the comparison of
# Actual and Predicted values.
df1.plot(
kind="bar", figsize=(16, 10), ax=ax1
)
# Though my model is not very precise, some predicted
# percentages are close to the actual ones.
# Plot the linear regression line
ax2.scatter([1,2,1], [1,2,3])
# Create a range of points. Compute
# yhat=coeff1*x + intercept
# and plot the regression line.
ax2.plot([1,2,1], color="red")
plt.legend(["Regression line", "data"])
plt.show()