On Wikipedia, there is a graph shown:
It shows the quadratic \$y = ax2 + bx + c\$, varying each coefficient separately while the other coefficients are fixed (at values a = 1, b = 0, c = 0)
As a learning experience I decided to replicate these plots in Matplotlib as follows:
import numpy as np import matplotlib.pyplot as plt import math #Plot the quadratic function y = ax2 + bx + c #Varying each coefficient [a, b, c] separately while the other coefficients are fixed (at values a = 1, b = 0, c = 0) #Iterate these 5 coeficients and plot each line coefs = [-2, -1, 0, 1, 2] #set up the plot and 3 subplots (to show the effect of varying each coefficient) f, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True, figsize=(18, 6)) #some x values to plot x = np.linspace(-2, 2, 30) for idx, val in enumerate([ax1, ax2, ax3]): for v in coefs: a, b, c = 1, 0, 0 if idx == 0: a = v elif idx == 1: b = v else: c = v y = a * (x**2) + (b * x) + c val.plot(x, y, label="Coeficient is " + str(coefs[i])) val.axhline(y=0, color='k') val.axvline(x=0, color='k') val.grid() val.legend(loc='lower center') plt.show()
It works fine:
but I am new to programming and I have an uneasy feeling that using
if statements isn't optimal. It feels like I should be iterating over something rather than using
What other ways could I iterate over the coefficients a, b & c to produce the 3 different subplots?