繁体   English   中英

Plot正弦波(度)

[英]Plot sine wave (degrees)

我们应该在 Matplotlib 的帮助下 plot 正弦波,但以度为单位。

首先,我们应该从 0 到 360 度,每 10 度获取每个 sin(x)。

我是初学者,非常感谢您的帮助。

def getsin():
    for i in range(0,361,10):
        y=math.sin(math.radians(i))
        sinvalue.append(round(y,3)
sinvalue=[]
getsin()


x=np.linspace(0,360,37)
plot.plot(x,sinvalue)
plot.xlabel("x, degrees")
plot.ylabel("sin(x)")
plot.show()

您的代码几乎没问题(您错过了sinvalue.append(round(y,3)行的右括号),但可以完善它。

例如,从 function 内部更新全局变量(我的意思是sinvalue )通常被认为是不好的做法......我并不是说它不应该这样做,我说它应该尽可能避免。

更常见的模式是让 function 返回一个值......

def getsin():
    sines = []
    for i in range(0,361,10):
        y=math.sin(math.radians(i))
        sines.append(round(y,3))
    return sines

为什么我说这个更好? 相比

sinvalue=[]
getsin()

sinvalue = getsin()

至少在我看来,第二个版本更好,因为它清楚地说明了正在发生的事情,现在sinvalue sinvalue是得到正弦的结果......

暂无
暂无

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

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