我创建了一个执行线性和多项式回归的小程序。 目前,两个回归都在同一个图表上。我想把它们放到两个不同的图形(制作子图),但我也希望有不同的标题(线性回归和多项式回归)。
我不知道该怎么做,能帮帮我吗?
import numpy as np
import matplotlib.pyplot as plt
import math
X = np.array([1,2,3,5,6,7,4,7,8,9,5,10,11,7,6,6,10,11,11,12,13,13,14])
Y = np.array([2,3,5,8,11,14,9,19,15,19,15,16,14,7,13,13,14,13,23,25,26,27,33])
#_______________ polinomial ________________
koeficienti_polinom = np.polyfit(X, Y, 2)
a=koeficienti_polinom[0]
b=koeficienti_polinom[1]
c=koeficienti_polinom[2]
xval=np.linspace(np.min(X), np.max(X))
regression=a * xval**2 + b*xval + c
predX = float(input("Enter: "))
predY = a * predX**2 + b*predX + c
plt.scatter(X,Y, s=20, color="blue" )
plt.scatter(predX, predY, color="red")
plt.plot(xval, regression, color="black", linewidth=1)
print("Polynomial: ",round(predY,2))
#________________ linear _________________
koeficienti_linear = np.polyfit(X, Y, 1)
a1=koeficienti_linear[0]
b1=koeficienti_linear[1]
xval1=np.linspace(np.min(X), np.max(X))
regression1=a1 * xval1 + b1
predY1 = a1 * predX+ b1
plt.scatter(predX, predY1, color="red")
plt.subplot(2,2,2)
plt.plot(xval1, regression1, color="black", linewidth=1)
plt.xlabel("Size")
plt.ylabel("Costs")
plt.title("Cost Prediction Graph")
plt.grid()
print("Linear: ", round(predY1,2))
plt.show()
答案 0 :(得分:0)
您想要使用matplotlib的subplots
命令。您传递plt.subplots
所需的行数和列数。它将在元组中返回它使用指定行数和列数创建的数字,以及一个"轴数组"这是由num cols组成的维数为num行的numpy
数组。您可以访问轴数组中的一组特定轴,例如访问numpy数组。
常见的语法是这样的:
fig, axarr = plt.subplots(2, 2) # generate figure and axis array
axarr[0][0].plot(some_x_data, some_y_data)
axarr[0][0].set_title("Upper left-hand plot")
axarr[0][0].set_xlabel("x")
axarr[0][0].set_ylabel("y")
axarr[0][1].scatter(some_x_data, some_y_data)
axarr[0][1].set_title("Upper right plot")
axarr[1][0].scatter(some_x_data, some_y_data)
axarr[1][0].set_title("Lower left plot")
axarr[1][1].scatter(some_x_data, some_y_data)
axarr[1][1].set_title("Lower right plot")
尝试初学者
#_______________ polinomial ________________
koeficienti_polinom = np.polyfit(X, Y, 2)
a=koeficienti_polinom[0]
b=koeficienti_polinom[1]
c=koeficienti_polinom[2]
xval=np.linspace(np.min(X), np.max(X))
regression=a * xval**2 + b*xval + c
predX = float(input("Enter: "))
predY = a * predX**2 + b*predX + c
fig, axarr = plt.subplots(2, 2)
axarr[0][0].scatter(X,Y, s=20, color="blue" )
axarr[0][0].scatter(predX, predY, color="red")
axarr[0][0].plot(xval, regression, color="black", linewidth=1)
axarr[0][0].set_title("Polynomial")
axarr[0][0].grid()
print("Polynomial: ",round(predY,2))
#________________ linear _________________
koeficienti_linear = np.polyfit(X, Y, 1)
a1=koeficienti_linear[0]
b1=koeficienti_linear[1]
xval1=np.linspace(np.min(X), np.max(X))
regression1=a1 * xval1 + b1
predY1 = a1 * predX+ b1
# idk what this plot is but its just a dot.
axarr[0][1].scatter(predX, predY1, color="red")
axarr[0][1].set_title("predX vs predY1")
axarr[0][1].set_xlabel("predX")
axarr[0][1].set_ylabel("predY1")
# this one looks like its the linear one you want
axarr[1][0].plot(xval1, regression1, color="black", linewidth=1)
axarr[1][0].set_xlabel("Size")
axarr[1][0].set_ylabel("Costs")
axarr[1][0].set_title("Cost Prediction Graph")
axarr[1][0].grid()
fig.tight_layout()
print("Linear: ", round(predY1, 2))
plt.show()