Matplotlib giving the wrong plot

Question:

I made a linear regression model and I want to plot the predicted results against the actual ones, so I wrote the following:

x = df['Years of Experience'].values
y = df['Salary'].values
for i in range(len(x)): #had a type error so did this
    x[i]=float(x[i])
beta1=regressor.coef_[0]
beta0=regressor.intercept_[0]
ypred = beta1*x + beta0
plt.plot(x,y,'o')                   
plt.plot(x,ypred,'-',linewidth=3) 
plt.xlabel('Years of Experience')
plt.ylabel('Salary')

I imported the following libraries:

import pandas as pd, numpy as np, matplotlib.pyplot as plt, matplotlib
%matplotlib inline

I made sure that all variables (beta and ypred) have values as expected but there seems to be something wrong with the plot as it gave the following:

enter image description here

Asked By: Sergio

||

Answers:

It feels like a matter of Y-axis range ..

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.plot(x,y,'o')                   
ax2.plot(x,ypred,'-',linewidth=3) 

plt.show()
Answered By: Jib
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.