import matplotlib.pyplot as plt
x_coords = []
y_coords = []
def myFunction(x):
return (3*(x**2)) + (6*x) + 9
def anotherFunction(x):
return (x***3) + (x**2) + x
def getCoords(fun, num):
for n in range(num):
x_coords.append(n)
y_coords.append(fun(n))
def draw_graph(x, y):
plt.plot(x, y, marker="o")
plt.show()
if __name__ == "__main__":
# myFunction needs an argument,
# getCoords provides it as num
getCoords(myFunction(), 42)
draw_graph(x_coords, y_coords)
getCoords(anotherFunction(), 69)
draw_graph(x_coords, y_coords)
I want to plot multiple arbitrary math functions while (ideally?) reusing code for getting coordinates and plotting them. Would there be a better way to restructure this, or am I super close to getting this working?
This question has great answers, but I'm not sure how to integrate them.