検索: fill_between matplot sabopy.com
matplotlib.collections — Matplotlib 3.6.0 documentation
# https://helve-blog.com/posts/python/numpy-fast-fourier-transform/ # https://sabopy.com/py/matplotlib-110/ import numpy as np import matplotlib.pyplot as plt N = 1024 # サンプル数 dt = 0.001 # サンプリング周期 [s] f1, f2 = 50, 120 # 周波数 [Hz] def func(t): return 1.5*np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t) + 3 # 信号 t = np.arange(0, N*dt, dt) # 時間 [s] print(t) #x = 1.5*np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t) + 3 # 信号 x =func(t) fig, ax = plt.subplots() ax.plot(t, x) # ax.set_xlim(0, 0.1) ax.set_xlabel("Time [s]") ax.set_ylabel("Signal") ax.grid() #t_ = np.arange(0.04, 0.6, dt) #ax.fill_between(t_,func(t_),fc="0.8",ec="0.5") plt.show() F = np.fft.fft(x) # 変換結果 freq = np.fft.fftfreq(N, d=dt) # 周波数 fig, ax = plt.subplots(nrows=3, sharex=True, figsize=(6,6)) ax[0].plot(F.real, label="Real part") ax[0].legend() ax[1].plot(F.imag, label="Imaginary part") ax[1].legend() ax[2].plot(freq, label="Frequency") ax[2].legend() ax[2].set_xlabel("Number of data") plt.show() Amp = np.abs(F/(N/2)) # 振幅 fig, ax = plt.subplots() ax.plot(freq[1:int(N/2)], Amp[1:int(N/2)]) ax.set_xlabel("Freqency [Hz]") ax.set_ylabel("Amplitude") ax.grid() #ax.fill_between(freq[1:int(N/2)],Amp[1:int(N/2)]) ax.fill_between(freq[5:50],Amp[5:50],fc="tab:blue") plt.show()
もしくはstemをつかう。