简体   繁体   English

Plot正弦波(度)

[英]Plot sine wave (degrees)

We're supposed to plot the sine wave with the help of Matplotlib but in degrees.我们应该在 Matplotlib 的帮助下 plot 正弦波,但以度为单位。

First we're supposed to get every sin(x) from 0-360 degrees, every ten degrees.首先,我们应该从 0 到 360 度,每 10 度获取每个 sin(x)。

I'm a beginner, would really appreciate the help.我是初学者,非常感谢您的帮助。

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

Your code is almost OK (you missed a closing parenthesis on line sinvalue.append(round(y,3) ), but it can be perfected.您的代码几乎没问题(您错过了sinvalue.append(round(y,3)行的右括号),但可以完善它。

Eg, it's usually considered bad practice updating a global variable (I mean sinvalue ) from inside a function... I don't say that it should never be done, I say that it should be avoided as far as possible.例如,从 function 内部更新全局变量(我的意思是sinvalue )通常被认为是不好的做法......我并不是说它不应该这样做,我说它应该尽可能避免。

A more common pattern is to have the function to return a value...更常见的模式是让 function 返回一个值......

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

Why I say this is better?为什么我说这个更好? Compare相比

sinvalue=[]
getsin()

with

sinvalue = getsin()

At least in my opinion, the second version is waaay better because it makes clear what is happening, now sinvalue is clearly the result of getting the sines...至少在我看来,第二个版本更好,因为它清楚地说明了正在发生的事情,现在sinvalue sinvalue是得到正弦的结果......

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

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