How do I plot a spectrogram the same way that pylab's specgram() does? (Matplotlib)

To plot a spectrogram the same way that pylab's specgram() does, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Create t, s1, s2, nse, x, NEFT and Fs data points using numpy.
  • Create a new figure or activate an existing figure using subplots() method with nrows=2.
  • Plot t and x data points using plot() method.
  • Lay out a grid in current line style.
  • Set the X-axis margins.
  • Plot a spectrogram using specgram() method.
  • Lay out a grid in current line style with dotted linestyle and some other properties.
  • To display the figure, use show() method.

Example

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 

Output

Updated on: 2021-06-09T12:22:41+05:30

527 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements