【Python报错】TypeError: only size-1 arrays can be converted to Python scalars

问题描述

lf = lambda x: ((1 + math.cos(x * math.pi / 30)) / 2) * (1 - 0.01) + 0.01
x = np.arange(1,30,1)
plt.plot(x, lf(x))
plt.show()

如上所示代码,在执行到lf(x)时会报错

解决方法

使用np.vectorize()

lf = lambda x: ((1 + math.cos(x * math.pi / 30)) / 2) * (1 - 0.01) + 0.01
lf = np.vectorize(lf)
x = np.arange(1,30,1)
plt.plot(x, lf(x))
plt.show()

具体原因以后在探究吧(。•́︿•̀。)

参考

https://stackoverflow.com/questions/36680402/typeerror-only-length-1-arrays-can-be-converted-to-python-scalars-while-plot-sh
https://www.pythonpool.com/only-size-1-arrays-can-be-converted-to-python-scalars-error-solved/

posted @ 2022-04-22 18:31  Odd!O_o  阅读(691)  评论(0)    收藏  举报
Live2D