Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python Matplotlib에서 스펙트로그램 위에 X축 그리드를 배치하는 방법은 무엇입니까?

<시간/>

Python에서 스펙트로그램 위에 X축 그리드를 배치하려면 grid() 메서드를 사용하고 다음 단계를 수행할 수 있습니다. -

  • 그림 크기를 설정하고 서브플롯 사이 및 주변 여백을 조정합니다.
  • numpy를 사용하여 t, s1, s2, nse, x, NEFT 및 Fs 데이터 포인트를 생성합니다.
  • subplots()를 사용하여 새 그림을 만들거나 기존 그림을 활성화합니다. nrows=2인 메소드
  • plot()을 사용하여 t 및 x 데이터 포인트를 플로팅합니다. 방법.
  • 현재 선 스타일로 그리드를 배치합니다.
  • X축 여백을 설정합니다.
  • specgram()을 사용하여 스펙트로그램 플로팅 방법.
  • 점선 스타일 및 기타 속성을 사용하여 현재 선 스타일로 그리드를 배치합니다.
  • 그림을 표시하려면 show()를 사용하세요. 방법.

예시

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

dt = 0.0005
t = np.arange(0.0, 20.0, dt)

s1 = np.sin(2 * np.pi * 100 * t)
s2 = 2 * np.sin(2 * np.pi * 400 * t)
s2[t <= 10] = s2[12 <= t] = 0

nse = 0.01 * np.random.random(size=len(t))
x = s1 + s2 + nse
NFFT = 1024
Fs = int(1.0 / dt)

fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.plot(t, x)
ax1.grid(axis="x", ls="dotted", lw=2, color="red")
ax1.margins(x=0)

Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
ax2.grid(axis="x", ls="dotted", lw=2, color="red")
plt.show()

출력

Python Matplotlib에서 스펙트로그램 위에 X축 그리드를 배치하는 방법은 무엇입니까?