分段函数
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['xtick.labelsize'] = 15
mpl.rcParams['ytick.labelsize'] = 15
plt.rcParams['mathtext.fontset'] = 'cm'
fs = 100
t = np.arange(-40, 40, 1/fs)
interval1 = [1 if(k < -20) else 0 for k in t]
interval2 = [1 if(-20 <= k < 20) else 0 for k in t]
interval3 = [1 if(k >= 20) else 0 for k in t]
y = np.sin(2*np.pi*1*t)*interval1+np.sin(2*np.pi*3*t)*interval2+np.sin(2*np.pi*5*t)*interval3
plt.plot(t, y)
plt.savefig('分段函数.jpg')
plt.show()