Để vẽ sóng sin trong Python, chúng tôi sử dụng thư viện Numpy và thư viện matplotlib., we use NumPy library and Matplotlib library. Numpy là gói cơ bản để điện toán khoa học với Python. Matplotlib là một thư viện âm mưu 2D Python tạo ra các số liệu chất lượng xuất bản trong nhiều định dạng hardcopy và môi trường tương tác trên các nền tảng. is the fundamental package for scientific computing with Python. Matplotlib is
a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Trong chương trình Python này, chúng tôi sử dụng Numpy để tạo các giá trị
5 và tính 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]
6 cho dữ liệu hình sin. Và matplotlib được sử dụng để xử lý âm mưu dữ liệu và định cấu hình các thuộc tính lô như nhãn, lưới, v.v.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]
Mã nguồn Python: Hàm sin
# Importing Required Libraries
import numpy as np
import matplotlib.pyplot as plt
# Generating time data using arange function from numpy
time = np.arange[-3*np.pi, 3*np.pi, 0.01]
# Finding amplitude at each time
amplitude = np.sin[time]
# Plotting time vs amplitude using plot function from pyplot
plt.plot[time, amplitude]
# Settng title for the plot in blue color
plt.title['Sine Wave', color='b']
# Setting x axis label for the plot
plt.xlabel['Time'+ r'$\rightarrow$']
# Setting y axis label for the plot
plt.ylabel['Sin[time] '+ r'$\rightarrow$']
# Showing grid
plt.grid[]
# Highlighting axis at x=0 and y=0
plt.axhline[y=0, color='k']
plt.axvline[x=0, color='k']
# Finally displaying the plot
plt.show[]
Đầu ra sóng sin
Sóng sin python: exaplanation [bước khôn ngoan]
- Bước 1: Nhập mã thư viện
7 và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]
8 được sử dụng để nhập thư viện Numpy và matplotlib.pyplot. Codefrom math import* Fs=8000 f=500 sample=16 a=[0]*sample for n in range[sample]: a[n]=sin[2*pi*f*n/Fs]
7 andfrom math import* Fs=8000 f=500 sample=16 a=[0]*sample for n in range[sample]: a[n]=sin[2*pi*f*n/Fs]
8 are used to import numpy and matplotlib.pyplot Library.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]
- Bước 2: Tạo mã dữ liệu thời gian
9 được sử dụng để tạo mẫu dữ liệu -3*π đến 3*π trong khoảng bằng 0,01. Hàmfrom math import* Fs=8000 f=500 sample=16 a=[0]*sample for n in range[sample]: a[n]=sin[2*pi*f*n/Fs]
0 được xác định trong Numpy. Codeimport 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[]
9 is used to generate data form -3*π to 3*π in equal interval of 0.01. Functionfrom math import* Fs=8000 f=500 sample=16 a=[0]*sample for n in range[sample]: a[n]=sin[2*pi*f*n/Fs]
0 is defined in numpy.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[]
- Bước 3: Tạo mã dữ liệu biên độ
1 được sử dụng để tính giá trị sin tại mỗi dữ liệu thời gian được tạo. Hàmimport 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[]
2 cũng được xác định trong Numpy. Codeimport 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[]
1 is used to calculate sine value at each generated time data. Functionimport 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[]
2 is also defined in numpy.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[]
- Bước 4: Đồ thị mã
3 được sử dụng để vẽ dữ liệu được tạo về thời gian và biên độ. Thời gian ở đây là dữ liệu trục x và biên độ là dữ liệu trục y. Codeimport 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[]
3 is used to plot generated data of time and amplitude. Here time is x-axis data and amplitude is y-axis data.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[]
- Bước 5: Đặt mã tiêu đề
4 được sử dụng để đặt tiêu đề của hình được tạo bằng màu xanh.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[]
5 chỉ ra tiêu đề có màu xanh lam. Codeimport 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[]
4 is used to set title of the generated figure in blue color.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[]
5 indicates title is in Blue Color.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[]
- Bước 6: Đặt mã nhãn
6 và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[]
7 được sử dụng để đặt nhãn cho trục x và trục y. Ở đâyimport 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[]
8 được sử dụng để tạo mũi tên phải → được nối với thời gian và biên độ bằng toán tử +. Codeimport 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[]
6 andimport 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[]
7 are used to set labels for x-axis and y-axis. Hereimport 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[]
8 is used to generate right arrow → which is concatenated with Time and Amplitude using + operator.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[]
- Bước 7: Hiển thị các lưới trong mã hình
9 được sử dụng để hiển thị lưới trong biểu đồ được tạo. Codeimport 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[]
9 is used to display grid in generated plot.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[]
- Bước 8: Làm nổi bật mã dòng gốc
0 vàimport matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
1 được sử dụng để ánh sáng cao x = 0 dòng và y = 0 dòng trong biểu đồ được tạo. Codeimport matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
0 andimport matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
1 are used to high light x=0 line and y=0 line in generated plot.import matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
- Bước 9: Cuối cùng hiển thị mã hình
2 được sử dụng để hiển thị biểu đồ cuối cùng trong đầu ra. Codeimport matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
2 is used to display final plot in output.import matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
Tôi có tín hiệu này:
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]
Làm thế nào tôi có thể vẽ đồ thị [sóng hình sin này]?
và tạo tên của xlabel là 'điện áp [v]' và ylabel là 'sample [n]'
Mã nào để làm điều này?
Tôi rất cảm ơn sự giúp đỡ ^_ ^
Đã hỏi ngày 21 tháng 3 năm 2014 lúc 18:25Mar 21, 2014 at 18:25
- Đặt
3 vớiimport matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
4 cho một mảng từ 0 đến 1 trong 0,001.import matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
5 Trả về một mảng 1000 điểm từ 0 đến 1 vàimport matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
6 bạn sẽ nhận được sóng tội lỗi từ 0 đến 1 được lấy mẫu 1000 lầnimport matplotlib.pyplot as plt # For ploting import numpy as np # to work with numerical data efficiently fs = 100 # sample rate f = 2 # the frequency of the signal x = np.arange[fs] # the points on the x axis for plotting # compute the value [amplitude] of the sin wave at the for each sample y = np.sin[2*np.pi*f * [x/fs]] #this instruction can only be used with IPython Notbook. % matplotlib inline # showing the exact location of the smaples plt.stem[x,y, 'r', ] plt.plot[x,y]
Hy vọng điều này có thể giúp cho bạn:
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[]
P.S .: Đối với công việc thoải mái, bạn có thể sử dụng máy tính xách tay Jupyter.
Đã trả lời ngày 22 tháng 3 năm 2014 lúc 10:51Mar 22, 2014 at 10:51
Nikioanikioanikioa
7215 Huy hiệu bạc6 Huy hiệu đồng5 silver badges6 bronze badges
0
import matplotlib.pyplot as plt # For ploting
import numpy as np # to work with numerical data efficiently
fs = 100 # sample rate
f = 2 # the frequency of the signal
x = np.arange[fs] # the points on the x axis for plotting
# compute the value [amplitude] of the sin wave at the for each sample
y = np.sin[2*np.pi*f * [x/fs]]
#this instruction can only be used with IPython Notbook.
% matplotlib inline
# showing the exact location of the smaples
plt.stem[x,y, 'r', ]
plt.plot[x,y]
Đã trả lời ngày 23 tháng 12 năm 2015 lúc 19:49Dec 23, 2015 at 19:49
AmjadamjadAmjad
2.7302 Huy hiệu vàng15 Huy hiệu bạc18 Huy hiệu đồng2 gold badges15 silver badges18 bronze badges
3
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[]
Đã trả lời ngày 29 tháng 11 năm 2019 lúc 12:03Nov 29, 2019 at 12:03
import math
import turtle
ws = turtle.Screen[]
ws.bgcolor["lightblue"]
fred = turtle.Turtle[]
for angle in range[360]:
y = math.sin[math.radians[angle]]
fred.goto[angle, y * 80]
ws.exit title[]
Đã trả lời ngày 28 tháng 2 năm 2015 lúc 17:46Feb 28, 2015 at 17:46
Cửa sổ hữu ích có thể đã đến và biến mất, nhưng tôi đã làm việc với một vấn đề tương tự. Đây là nỗ lực của tôi trong việc vẽ hình sin bằng mô -đun rùa.
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]]]
Đã trả lời ngày 5 tháng 7 năm 2014 lúc 23:56Jul 5, 2014 at 23:56
Một cách đơn giản để vẽ sóng sin trong Python bằng cách sử dụng matplotlib.
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[]
lbsweek
4.73443 Huy hiệu bạc43 Huy hiệu Đồng43 silver badges43 bronze badges
Đã trả lời ngày 2 tháng 2 năm 2019 lúc 17:14Feb 2, 2019 at 17:14
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[]
Đã trả lời ngày 4 tháng 6 năm 2019 lúc 13:36Jun 4, 2019 at 13:36
1
Đây là một tùy chọn khác
#!/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[]
Đã trả lời ngày 25 tháng 7 năm 2020 lúc 17:39Jul 25, 2020 at 17:39
JoaquimjoaquimJoaquim
3303 Huy hiệu bạc8 Huy hiệu Đồng3 silver badges8 bronze badges
1
Tuy nhiên, một cách khác để âm mưu sóng hình sin.
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]
0Đã trả lời ngày 25 tháng 7 năm 2020 lúc 18:07Jul 25, 2020 at 18:07
JoaquimjoaquimJoaquim
3303 Huy hiệu bạc8 Huy hiệu Đồng3 silver badges8 bronze badges
1
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]
1Tuy nhiên, một cách khác để âm mưu sóng hình sin.
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]
2Đã trả lời ngày 25 tháng 7 năm 2020 lúc 18:07
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]
3Tạo tọa độ X
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]
4nhập matplotlib để vẽ đồ thịFeb 18 at 8:56