I am trying to plot a function which is defined via a lambda
. I always get the error message:
x and y must have the same first dimension but have shapes (20,) and (1,)
But I have no idea why.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4,8,20)
print(x)
fx = lambda x: x - 10 * np.exp(-1/10*((x-2)**2))
plt.plot(x, fx)
plt.show()
CodePudding user response:
fx
is a lambda
function, x
is a list.
You shall first compute all the results and then pass it to the plot:
x = np.linspace(-4,8,20)
fx = lambda x: x - 10 * np.exp(-1/10*((x-2)**2))
y = [fx(val) for val in x] # Not sure, maybe you need to use Numpy too instead ? Not a expert of Numpy
plt.plot(x, y)