0

I created a small program that does linear and polynomial regression. For now, both regressions are on the same graph. I want to put them to two different graphs (to make subplots), but also I want to have different titles (Linear regression and Polynomial regression).

I do not know how to do this, can you please help me?

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()
Aleksandar Beat
  • 191
  • 7
  • 22

1 Answers1

0

You'll want to use matplotlib's subplots command. You pass plt.subplots the number of rows that you want and the number of columns. It will return in a tuple the figure that it has created with the specified number of rows and columns, and an "axis array" which is a numpy array of dimension num rows by num cols. You can access a specific set of axes in the axis array like accessing a numpy array.

The common syntax would be like this:

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")

Give this a try for starters

#_______________ 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()
Jon Deaton
  • 3,943
  • 6
  • 28
  • 41