得到'only length-1 arrays can be converted to Python scalars'错误

import matplotlib.pyplot as plt
import numpy as np
from math import sin, pi

y = pi
x = np.linspace(0, 3*y, 500)
plt.plot(x, sin(x**2))
title("A simple Graph")
plt.show()

越来越

'只有长度为1的数组可以转换为Python标量'

运行此代码时出错

回答(2)

2 years ago

只需将 plt.plot(x,sin(x**2)) 更改为 plt.plot(x,np.sin(x**2))

2 years ago

您可以使用此代码:

import matplotlib.pyplot as plt
import numpy as np
from math import sin,pi 
y=pi
x = np.linspace(0, 3*y, 500)
plt.plot(x,[sin(_**2) for _ in x])
title("A simple Graph")
plt.show()

情节函数的第二个参数不正确 .