Search code examples
pythonmathalgebra

How can I automate code to plot a line between two points?


Currently trying to create an automated function that can find a line between two points. Tried testing this function (by plotting) but when I plot the function I get a 'v-shaped' line. What changes can I make to complete this function? Thanks.

     import matplotlib.pyplot as plt
     from math import e

     x = [0, 9]
     y = [7, 2]
     m = (y[1]-x[1])/(y[0]-x[0])
     b = m*(y[0]-x[0])+x[1]

     for i in range (-10, 10):
      prob_x = m*i + b

      x.append(i)
      y.append(prob_x)

     plt.plot(x,y)

Solution

  • import matplotlib.pyplot as plt
    
    x = [0, 9]
    y = [7, 2]
    plt.plot(x, y)
    plt.show()
    

    You don't need to do anything, plt.plot returns a line between two points anyway

    Output:

    enter image description here