绘制lambda函数



我试图绘制一个通过lambda定义的函数。我总是得到错误信息:

x and y must have the same first dimension but have shapes (20,) and (1,)

但我不知道为什么。

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()

fxlambda函数,x为列表。首先计算所有结果,然后将其传递给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)

相关内容

  • 没有找到相关文章