Basic python question Define a function plotpolynomial xs ys
Basic python question
Define a function plot_polynomial( xs, ys, degree, color ) that takes in two lists of coordinates, a degree, and a string representing plot color.
This function should:
Call polynomial_fit( xs, ys, degree ) to get fitted_ys.
Plot the given xs and ys with using a dot marker for the given color.
Plot fitted_ys on xs with lines of the given color.
Example Usage:
>>> xs = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> ys = [ 4, 7, 12, 16, 10, 10, 9, 15, 17, 19, 10, 8, 6, 5, 2, 1]
>>> plot_polynomial(xs, ys, 5, \'b\')
>>> plt.show()
~a graph pops up as well that is 20 high, 16 wide, has a hill like line running through with random dots
Solution
import numpy as np
import matplotlib.pyplot as plt
def plot_polynomial(xs, ys, degree, color):
polynomial = np.poly1d(np.polyfit(np.array(xs), np.array(ys), degree))
plt.plot(polynomial(xs),ys,color)
plt.show()
