Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Basic Python Programming USE NUMPY PLEASE Define a function plot_multiple_polyno

ID: 3776032 • Letter: B

Question

Basic Python Programming USE NUMPY PLEASE

Define a function plot_multiple_polynomials( xs, ys, degree, colors ) that takes in two lists of coordinates, a degree, and a list of strings representing plot colors.

This function should:

Plot the given xs and ys with colors[0] and a label.

For every degree from zero to degree:

Call polynomial_fit( xs, ys, degree ) to get fitted_ys.

Plot fitted_ys on xs with colors[degree+1] and a label.

Label every plotted line and generate a legend.

def polynomial_fit(xs, ys, degree):
return(np.poly1d(np.polyfit(np.array(xs), np.array(ys), degree))(xs))

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_multiple_polynomials(xs, ys, 5, ['mo','y--','g--','k--','r--','b--','m'])
>>> plt.show()

Explanation / Answer

"""
FileName:plot_multiple_polynomial.py
"""
import numpy as np
import matplotlib.pyplot as plt
def polynomial_fit(xs, ys, degree):
return np.poly1d(np.polyfit(np.array(xs), np.array(ys), degree))(xs)

def plot_multiple_polynomials(xs, ys, degree, colors):
"""
Summary line.

This function will plot the multiple ploynomials. it will take coordinate,
a degree and list of string represent the plot color

Parameters
----------
xs:list
This is X axis.
ys:list
This Y axis.
degree:int
This is maximum degree for ploynomials which we plot (0,degree)

colors:list
This will conatin list of string ,each string contain two characters first
character will represent color and second one represent style of line

"""

for x in range(degree+1):
fitted_ys = polynomial_fit(xs, ys, x)
plt.plot(xs, fitted_ys, colors[x+1], label='line '+str(x + 1))
plt.legend(loc=1)

X = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Y = [4, 7, 12, 16, 10, 10, 9, 15, 17, 19, 10, 8, 6, 5, 2, 1]
plot_multiple_polynomials(X, Y, 5, ['mo', 'y--', 'g--', 'k--', 'r--', 'b--', 'm'])
plt.show()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote