I want to plot any mathematical function such as y=x**2 or y=sin(x)/x (anything basically).
The problem is, I can only plot functions defined in numpy such as np.sin, np.cos or np.tan.
The entire point here is how to avoid "hard coding" vectors and then plot those.
How can I plot arbitrary functions? Here is what I have so far:
import matplotlib.pyplot as plt
import numpy as np
#Let a be starting point on x
#Let b be end point on x
#Let f be the function you wish to plot
CornflowerBlue="6495ed"
def plotf(a,b,f):
x=np.linspace(a,b,num=b*10)
y=np.array([])
for i in range (len(x)):
y = np.append(y,f(x[i]))
plt.grid(True)
plt.plot(x,y,color="CornflowerBlue")
plt.axhline(y=0,color="black")
return
I can only plot functions defined in numpy such as np.sin, np.cos or np.tan. What do you mean by that? If i useplotf(-5, 5, lambda x: x**2 + 1)and thenplt.show()it correctly renders a function that is not defined in numpy.def f(x): return x**2 + 1withplt.show()andplotf(-5, 5, f)works the same, I still don't get what your question is.y = x**2 + 1is all you need to do