Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python如何绘制正弦波图_Python 2.7_Plot - Fatal编程技术网

Python 2.7 Python如何绘制正弦波图

Python 2.7 Python如何绘制正弦波图,python-2.7,plot,Python 2.7,Plot,我有这个信号: from math import* Fs=8000 f=500 sample=16 a=[0]*sample for n in range(sample): a[n]=sin(2*pi*f*n/Fs) 如何绘制图形(此正弦波) 并将xlabel的名称创建为“电压(V)”,将ylabel的名称创建为“样本(n)” 什么代码可以这样做 非常感谢你的帮助 使用np.arange(0,1,0.001)设置x轴,以0.001的增量给出从0到1的数组。 x=np.arange(0

我有这个信号:

from math import*
Fs=8000
f=500
sample=16
a=[0]*sample
for n in range(sample):
    a[n]=sin(2*pi*f*n/Fs)
如何绘制图形(此正弦波)

并将xlabel的名称创建为“电压(V)”,将ylabel的名称创建为“样本(n)”

什么代码可以这样做

非常感谢你的帮助
  • 使用
    np.arange(0,1,0.001)
    设置
    x轴
    ,以0.001的增量给出从0到1的数组。
    • x=np.arange(0,1,0.001)
      返回从0到1的1000个点的数组,并且
      y=np.sin(2*np.pi*x)
      您将获得从0到1的1000次采样的正弦波
  • 我希望这将有助于:

    import matplotlib.pyplot as plt
    import numpy as np
    
    
    Fs = 8000
    f = 5
    sample = 8000
    x = np.arange(sample)
    y = np.sin(2 * np.pi * f * x / Fs)
    plt.plot(x, y)
    plt.xlabel('sample(n)')
    plt.ylabel('voltage(V)')
    plt.show()
    


    注:对于舒适的工作,你可以使用。

    有用的窗口可能来了又走了,但我正在处理类似的问题。下面是我使用turtle模块绘制正弦曲线的尝试

    from turtle import *
    from math import *
    
    #init turtle
    T=Turtle()
    
    #sample size
    T.screen.setworldcoordinates(-1,-1,1,1) 
    
    #speed up the turtle
    T.speed(-1)
    
    #range of hundredths from -1 to 1
    xcoords=map(lambda x: x/100.0,xrange(-100,101))
    
    #setup the origin
    T.pu();T.goto(-1,0);T.pd()
    
    #move turtle
    for x in xcoords:
        T.goto(x,sin(xcoords.index(x)))
    

    使用matplotlib在python中绘制正弦波的简单方法

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    x=np.arange(0,3*np.pi,0.1)
    y=np.sin(x)
    plt.plot(x,y)
    plt.title("SINE WAVE")
    plt.show()
    
    这是另一种选择

    #!/usr/bin/env python
    
    import numpy as np
    import matplotlib
    matplotlib.use('TKAgg') #use matplotlib backend TkAgg (optional)
    import matplotlib.pyplot as plt
    
    sample_rate = 200 # sampling frequency in Hz (atleast 2 times f)
    t = np.linspace(0,5,sample_rate)    #time axis
    f = 100 #Signal frequency in Hz
    sig = np.sin(2*np.pi*f*(t/sample_rate))
    plt.plot(t,sig)
    plt.xlabel("Time")
    plt.ylabel("Amplitude")
    plt.tight_layout() 
    plt.show()
    

    另一种绘制正弦波的方法

    import numpy as np
    import matplotlib
    matplotlib.use('TKAgg') #use matplotlib backend TKAgg (optional)
    import matplotlib.pyplot as plt
    
    t = np.linspace(0.0, 5.0, 50000)       # time axis
    sig = np.sin(t)
    plt.plot(t,sig)
    

    %matplotlib inline
    此指令只能与IPython Notbook一起使用。整数除法只能产生python 3之前的整数,因此
    i/fs
    仅在python 3上运行时产生浮点,对于python 2,将
    fs=100.0
    设置为解决方法
    [np.sin(2*np.pi*f*(i/fs)]用于x中的i]
    可以简化为
    np.sin(2*np.pi*f*(x/fs))
    。欢迎使用堆栈溢出!一般来说,如果答案中包含对代码的用途的解释,以及在不介绍其他人的情况下解决问题的原因,那么答案会更有帮助。如果添加解释而不是仅添加代码,则答案会更有帮助。如果添加解释而不是仅添加代码,则答案会更有帮助。
    import matplotlib.pyplot as plt
    import numpy as np
    #%matplotlib inline
    x=list(range(10))
    def fun(k):
         return np.sin(k)
    y=list(map(fun,x))
    plt.plot(x,y,'-.')
    #print(x)
    #print(y)
    plt.show()
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    F = 5.e2          # No. of cycles per second, F = 500 Hz
    T = 2.e-3         # Time period, T = 2 ms
    Fs = 50.e3        # No. of samples per second, Fs = 50 kHz
    Ts = 1./Fs        # Sampling interval, Ts = 20 us
    N = int(T/Ts)     # No. of samples for 2 ms, N = 100
    
    t = np.linspace(0, T, N)
    signal = np.sin(2*np.pi*F*t)
    
    plt.plot(t, signal)
    plt.xlabel('Time (s)')
    plt.ylabel('Voltage (V)')
    plt.show()
    
    #!/usr/bin/env python
    
    import numpy as np
    import matplotlib
    matplotlib.use('TKAgg') #use matplotlib backend TkAgg (optional)
    import matplotlib.pyplot as plt
    
    sample_rate = 200 # sampling frequency in Hz (atleast 2 times f)
    t = np.linspace(0,5,sample_rate)    #time axis
    f = 100 #Signal frequency in Hz
    sig = np.sin(2*np.pi*f*(t/sample_rate))
    plt.plot(t,sig)
    plt.xlabel("Time")
    plt.ylabel("Amplitude")
    plt.tight_layout() 
    plt.show()
    
    import numpy as np
    import matplotlib
    matplotlib.use('TKAgg') #use matplotlib backend TKAgg (optional)
    import matplotlib.pyplot as plt
    
    t = np.linspace(0.0, 5.0, 50000)       # time axis
    sig = np.sin(t)
    plt.plot(t,sig)