- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 <= 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()
Output
- Related Articles
- Which is the recommended way to plot – matplotlib or pylab?
- How do I plot only a table in Matplotlib?
- How do I plot a step function with Matplotlib in Python?
- How do I let my Matplotlib plot go beyond the axes?
- How to plot 1D data at a given Y-value with PyLab using Matplotlib?
- How to use different markers for different points in a Pylab scatter plot(Matplotlib)?
- How do I remove the Y-axis from a Pylab-generated picture?
- How to check that pylab backend of Matplotlib runs inline?
- How do I plot Shapely polygons and objects using Matplotlib?
- How do I plot hatched bars using Pandas and Matplotlib?
- How do I plot multiple X or Y axes in Matplotlib?
- How can I plot two different spaced time series on one same plot in Python Matplotlib?
- How do I redraw an image using Python's Matplotlib?
- How do I show the same Matplotlib figure several times in a single IPython notebook?
- How do I change the axis tick font in a Matplotlib plot when rendering using LaTeX?

Advertisements