Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8729ed3

Browse files
authored
Merge pull request #26625 from meeseeksmachine/auto-backport-of-pr-26622-on-v3.8.x
Backport PR #26622 on branch v3.8.x ([Doc] Improve DSP-related examples)
2 parents 188711b + 3eef726 commit 8729ed3

File tree

6 files changed

+55
-47
lines changed

6 files changed

+55
-47
lines changed

galleries/examples/images_contours_and_fields/specgram_demo.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
================
3-
Spectrogram Demo
4-
================
2+
===========
3+
Spectrogram
4+
===========
55
6-
Demo of a spectrogram plot (`~.axes.Axes.specgram`).
6+
Plotting a spectrogram using `~.Axes.specgram`.
77
"""
88
import matplotlib.pyplot as plt
99
import numpy as np
@@ -12,7 +12,7 @@
1212
np.random.seed(19680801)
1313

1414
dt = 0.0005
15-
t = np.arange(0.0, 20.0, dt)
15+
t = np.arange(0.0, 20.5, dt)
1616
s1 = np.sin(2 * np.pi * 100 * t)
1717
s2 = 2 * np.sin(2 * np.pi * 400 * t)
1818

@@ -24,16 +24,22 @@
2424

2525
x = s1 + s2 + nse # the signal
2626
NFFT = 1024 # the length of the windowing segments
27-
Fs = int(1.0 / dt) # the sampling frequency
27+
Fs = 1/dt # the sampling frequency
2828

29-
fig, (ax1, ax2) = plt.subplots(nrows=2)
29+
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
3030
ax1.plot(t, x)
31-
Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
31+
ax1.set_ylabel('Signal')
32+
33+
Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs)
3234
# The `specgram` method returns 4 objects. They are:
3335
# - Pxx: the periodogram
3436
# - freqs: the frequency vector
3537
# - bins: the centers of the time bins
3638
# - im: the .image.AxesImage instance representing the data in the plot
39+
ax2.set_xlabel('Time (s)')
40+
ax2.set_ylabel('Frequency (Hz)')
41+
ax2.set_xlim(0, 20)
42+
3743
plt.show()
3844

3945
# %%

galleries/examples/lines_bars_and_markers/cohere.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plotting the coherence of two signals
44
=====================================
55
6-
An example showing how to plot the coherence of two signals.
6+
An example showing how to plot the coherence of two signals using `~.Axes.cohere`.
77
"""
88
import matplotlib.pyplot as plt
99
import numpy as np
@@ -20,15 +20,14 @@
2020
s1 = np.sin(2 * np.pi * 10 * t) + nse1
2121
s2 = np.sin(2 * np.pi * 10 * t) + nse2
2222

23-
fig, axs = plt.subplots(2, 1)
23+
fig, axs = plt.subplots(2, 1, layout='constrained')
2424
axs[0].plot(t, s1, t, s2)
2525
axs[0].set_xlim(0, 2)
26-
axs[0].set_xlabel('Time')
26+
axs[0].set_xlabel('Time (s)')
2727
axs[0].set_ylabel('s1 and s2')
2828
axs[0].grid(True)
2929

3030
cxy, f = axs[1].cohere(s1, s2, 256, 1. / dt)
3131
axs[1].set_ylabel('Coherence')
3232

33-
fig.tight_layout()
3433
plt.show()

galleries/examples/lines_bars_and_markers/csd_demo.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
"""
2-
========
3-
CSD Demo
4-
========
2+
============================
3+
Cross spectral density (CSD)
4+
============================
55
6-
Compute the cross spectral density of two signals
6+
Plot the cross spectral density (CSD) of two signals using `~.Axes.csd`.
77
"""
88
import matplotlib.pyplot as plt
99
import numpy as np
1010

11-
fig, (ax1, ax2) = plt.subplots(2, 1)
12-
# make a little extra space between the subplots
13-
fig.subplots_adjust(hspace=0.5)
11+
fig, (ax1, ax2) = plt.subplots(2, 1, layout='constrained')
1412

1513
dt = 0.01
1614
t = np.arange(0, 30, dt)
@@ -32,10 +30,11 @@
3230

3331
ax1.plot(t, s1, t, s2)
3432
ax1.set_xlim(0, 5)
35-
ax1.set_xlabel('Time')
33+
ax1.set_xlabel('Time (s)')
3634
ax1.set_ylabel('s1 and s2')
3735
ax1.grid(True)
3836

3937
cxy, f = ax2.csd(s1, s2, 256, 1. / dt)
4038
ax2.set_ylabel('CSD (dB)')
39+
4140
plt.show()

galleries/examples/lines_bars_and_markers/psd_demo.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
========
3-
Psd Demo
4-
========
2+
============================
3+
Power spectral density (PSD)
4+
============================
55
6-
Plotting Power Spectral Density (PSD) in Matplotlib.
6+
Plotting power spectral density (PSD) using `~.Axes.psd`.
77
88
The PSD is a common plot in the field of signal processing. NumPy has
99
many useful libraries for computing a PSD. Below we demo a few examples
@@ -26,8 +26,10 @@
2626
cnse = cnse[:len(t)]
2727
s = 0.1 * np.sin(2 * np.pi * t) + cnse
2828

29-
fig, (ax0, ax1) = plt.subplots(2, 1)
29+
fig, (ax0, ax1) = plt.subplots(2, 1, layout='constrained')
3030
ax0.plot(t, s)
31+
ax0.set_xlabel('Time (s)')
32+
ax0.set_ylabel('Signal')
3133
ax1.psd(s, 512, 1 / dt)
3234

3335
plt.show()
@@ -64,8 +66,8 @@
6466
], layout='constrained')
6567

6668
axs['signal'].plot(t, y)
67-
axs['signal'].set_xlabel('time [s]')
68-
axs['signal'].set_ylabel('signal')
69+
axs['signal'].set_xlabel('Time (s)')
70+
axs['signal'].set_ylabel('Signal')
6971

7072
# Plot the PSD with different amounts of zero padding. This uses the entire
7173
# time series at once
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
========================
3-
Spectrum Representations
3+
Spectrum representations
44
========================
55
66
The plots show different spectrum representations of a sine signal with
@@ -24,28 +24,28 @@
2424

2525
s = 0.1 * np.sin(4 * np.pi * t) + cnse # the signal
2626

27-
fig, axs = plt.subplots(nrows=3, ncols=2, figsize=(7, 7))
27+
fig = plt.figure(figsize=(7, 7), layout='constrained')
28+
axs = fig.subplot_mosaic([["signal", "signal"],
29+
["magnitude", "log_magnitude"],
30+
["phase", "angle"]])
2831

2932
# plot time signal:
30-
axs[0, 0].set_title("Signal")
31-
axs[0, 0].plot(t, s, color='C0')
32-
axs[0, 0].set_xlabel("Time")
33-
axs[0, 0].set_ylabel("Amplitude")
33+
axs["signal"].set_title("Signal")
34+
axs["signal"].plot(t, s, color='C0')
35+
axs["signal"].set_xlabel("Time (s)")
36+
axs["signal"].set_ylabel("Amplitude")
3437

3538
# plot different spectrum types:
36-
axs[1, 0].set_title("Magnitude Spectrum")
37-
axs[1, 0].magnitude_spectrum(s, Fs=Fs, color='C1')
39+
axs["magnitude"].set_title("Magnitude Spectrum")
40+
axs["magnitude"].magnitude_spectrum(s, Fs=Fs, color='C1')
3841

39-
axs[1, 1].set_title("Log. Magnitude Spectrum")
40-
axs[1, 1].magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')
42+
axs["log_magnitude"].set_title("Log. Magnitude Spectrum")
43+
axs["log_magnitude"].magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')
4144

42-
axs[2, 0].set_title("Phase Spectrum ")
43-
axs[2, 0].phase_spectrum(s, Fs=Fs, color='C2')
45+
axs["phase"].set_title("Phase Spectrum ")
46+
axs["phase"].phase_spectrum(s, Fs=Fs, color='C2')
4447

45-
axs[2, 1].set_title("Angle Spectrum")
46-
axs[2, 1].angle_spectrum(s, Fs=Fs, color='C2')
48+
axs["angle"].set_title("Angle Spectrum")
49+
axs["angle"].angle_spectrum(s, Fs=Fs, color='C2')
4750

48-
axs[0, 1].remove() # don't display empty ax
49-
50-
fig.tight_layout()
5151
plt.show()

galleries/examples/lines_bars_and_markers/xcorr_acorr_demo.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
================================
3-
Cross- and Auto-Correlation Demo
4-
================================
2+
===========================
3+
Cross- and auto-correlation
4+
===========================
55
66
Example use of cross-correlation (`~.Axes.xcorr`) and auto-correlation
77
(`~.Axes.acorr`) plots.
@@ -17,9 +17,11 @@
1717
fig, [ax1, ax2] = plt.subplots(2, 1, sharex=True)
1818
ax1.xcorr(x, y, usevlines=True, maxlags=50, normed=True, lw=2)
1919
ax1.grid(True)
20+
ax1.set_title('Cross-correlation (xcorr)')
2021

2122
ax2.acorr(x, usevlines=True, normed=True, maxlags=50, lw=2)
2223
ax2.grid(True)
24+
ax2.set_title('Auto-correlation (acorr)')
2325

2426
plt.show()
2527

0 commit comments

Comments
 (0)