简体   繁体   English

我无法为y = sin(x)创建图

[英]I am not able to create the graph for y=sin(x)

enter code here
import numpy as np  
import math  
import matplotlib.pylab as plt  
a=np.linspace(3,6,10)  
plt.plot(a,math.sin(a))  
plt.show()

The output says ****TypeError: only size-1 arrays can be converted to Python scalars 输出显示**** TypeError:只能将size-1数组转换为Python标量

Use np.sin or np.vectorize(math.sin) . 使用np.sinnp.vectorize(math.sin)


import numpy as np  
import math  
import matplotlib.pylab as plt  
a = np.linspace(3,6,10)  
plt.plot(a, np.sin(a))  
plt.show()

Note that np.sin , like math.sin , takes radians rather than degrees , so you may want to adjust your array ( a ) accordingly, or use np.rad2deg because at the moment the result is: 需要注意的是np.sin ,像math.sin ,采用弧度而不是 ,所以你可能需要调整您的阵列( a相应的),或者使用np.rad2deg ,因为在目前的结果是:

plt1

Whereas if you were to pass in floats between 0 and 2 * math.pi , you would get a nice sine wave: 而如果要在02 * math.pi之间传递浮点数,则会得到一个不错的正弦波:

plt2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM