Python 综合组织几个由“引用”连接的语句;或;
我想画一个图,在不同的时间显示几个脉冲。 例如,我有带有每个脉冲时间的列表Python 综合组织几个由“引用”连接的语句;或;,python,numpy,matplotlib,Python,Numpy,Matplotlib,我想画一个图,在不同的时间显示几个脉冲。 例如,我有带有每个脉冲时间的列表l l=[1.,2.,24.] 我有脉冲的持续时间,例如d=0.2。 我可以通过以下方式进行绘图: import numpy as np import matplotlib.pyplot as plt t=np.linspace(0.,30,1000) l=[1.,2.,24.] d=0.2 def pulse(t): if t<l[0]: L = 0. elif l[0]<
l
l=[1.,2.,24.]
我有脉冲的持续时间,例如d=0.2
。
我可以通过以下方式进行绘图:
import numpy as np
import matplotlib.pyplot as plt
t=np.linspace(0.,30,1000)
l=[1.,2.,24.]
d=0.2
def pulse(t):
if t<l[0]:
L = 0.
elif l[0]<=t<l[0]+d or l[1]<=t<l[1]+d or l[2]<=t<l[2]+d:
L = 1
else:
L=0.
return L
plt.figure(1)
P=map(pulse,t)
plt.plot(t,P)
plt.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
t=np.linspace(0,301000)
l=[1,2,24]
d=0.2
def脉冲(t):
如果t,您可以使用for循环测试几个不同的条件。例如:
def pulse(t):
L = 0
for i in range(len(l)):
if l[i] <= t < l[i] + d:
L += 1
return L
def脉冲(t):
L=0
对于范围内的i(len(l)):
if l[i]我认为可以使用numpy indexon提高效率,当然也可以为未知数量的if语句编写for循环
p = np.zeros(1000)
for i in l:
p[(0<=t-i) & (t-i<d)] = 1
p=np.零(1000)
对于l中的i:
p[(0)您可以使用for循环。