Home > front end >  Difference between graphs
Difference between graphs

Time:09-17

def PlotPolly(model, independent_variable, dependent_variabble, Name):
    x_new = np.linspace(15, 55, 100) 
    y_new = model(x_new)

    plt.plot(independent_variable, dependent_variabble, '.', x_new, y_new, '-') #4
    plt.title('Polynomial Fit with Matplotlib for Price ~ Length')
    ax = plt.gca()
    ax.set_facecolor((0.898, 0.898, 0.898))
    fig = plt.gcf()
    plt.xlabel(Name)
    plt.ylabel('Price of Cars')

    plt.show()
    plt.close()

I get this with this code:

enter image description here

But when from line 4 I remove x_new and y_new line becomes

plt.plot(independent_variable, dependent_variabble)

I get this graph :

enter image description here

Can you explain what is meaning of x_new and y_new and why absence of this results in this kind of graph

CodePudding user response:

In your code x_new and y_new both of them have the continuous values but independent_variable and dependent_variabble have ‍discontinuous values and for plot discontinues you need scatter plot. see this example:

import numpy as np
import matplotlib.pyplot as plt


x = np.array([2, 1, 5, 3, 4, 2, 6, 4])
y = np.array([3, 1, 2, 0, 1, 2, 6, 4])

plt.plot(x, y, linestyle='-', marker='o')

Output:

enter image description here

  • Related