|
12 | 12 | import matplotlib.pyplot as plt |
13 | 13 | import numpy as np |
14 | 14 | import matplotlib.mlab as mlab |
| 15 | +import matplotlib.gridspec as gridspec |
15 | 16 |
|
16 | 17 | # Fixing random state for reproducibility |
17 | 18 | np.random.seed(19680801) |
18 | 19 |
|
19 | | - |
20 | 20 | dt = 0.01 |
21 | 21 | t = np.arange(0, 10, dt) |
22 | 22 | nse = np.random.randn(len(t)) |
|
59 | 59 | y = y + np.random.randn(*t.shape) |
60 | 60 |
|
61 | 61 | # Plot the raw time series |
62 | | -fig = plt.figure() |
63 | | -fig.subplots_adjust(hspace=0.45, wspace=0.3) |
64 | | -ax = fig.add_subplot(2, 1, 1) |
| 62 | +fig = plt.figure(constrained_layout=True) |
| 63 | +gs = gridspec.GridSpec(2, 3, figure=fig) |
| 64 | +ax = fig.add_subplot(gs[0, :]) |
65 | 65 | ax.plot(t, y) |
| 66 | +ax.set_xlabel('time [s]') |
| 67 | +ax.set_ylabel('signal') |
66 | 68 |
|
67 | 69 | # Plot the PSD with different amounts of zero padding. This uses the entire |
68 | 70 | # time series at once |
69 | | -ax2 = fig.add_subplot(2, 3, 4) |
| 71 | +ax2 = fig.add_subplot(gs[1, 0]) |
70 | 72 | ax2.psd(y, NFFT=len(t), pad_to=len(t), Fs=fs) |
71 | 73 | ax2.psd(y, NFFT=len(t), pad_to=len(t) * 2, Fs=fs) |
72 | 74 | ax2.psd(y, NFFT=len(t), pad_to=len(t) * 4, Fs=fs) |
73 | 75 | plt.title('zero padding') |
74 | 76 |
|
75 | 77 | # Plot the PSD with different block sizes, Zero pad to the length of the |
76 | 78 | # original data sequence. |
77 | | -ax3 = fig.add_subplot(2, 3, 5, sharex=ax2, sharey=ax2) |
| 79 | +ax3 = fig.add_subplot(gs[1, 1], sharex=ax2, sharey=ax2) |
78 | 80 | ax3.psd(y, NFFT=len(t), pad_to=len(t), Fs=fs) |
79 | 81 | ax3.psd(y, NFFT=len(t) // 2, pad_to=len(t), Fs=fs) |
80 | 82 | ax3.psd(y, NFFT=len(t) // 4, pad_to=len(t), Fs=fs) |
81 | 83 | ax3.set_ylabel('') |
82 | 84 | plt.title('block size') |
83 | 85 |
|
84 | 86 | # Plot the PSD with different amounts of overlap between blocks |
85 | | -ax4 = fig.add_subplot(2, 3, 6, sharex=ax2, sharey=ax2) |
| 87 | +ax4 = fig.add_subplot(gs[1, 2], sharex=ax2, sharey=ax2) |
86 | 88 | ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t), noverlap=0, Fs=fs) |
87 | 89 | ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t), |
88 | 90 | noverlap=int(0.05 * len(t) / 2.), Fs=fs) |
|
106 | 108 | xn = (A * np.sin(2 * np.pi * f * t)).sum(axis=0) |
107 | 109 | xn += 5 * np.random.randn(*t.shape) |
108 | 110 |
|
109 | | -fig, (ax0, ax1) = plt.subplots(ncols=2) |
| 111 | +fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True) |
110 | 112 |
|
111 | | -fig.subplots_adjust(hspace=0.45, wspace=0.3) |
112 | 113 | yticks = np.arange(-50, 30, 10) |
113 | 114 | yrange = (yticks[0], yticks[-1]) |
114 | 115 | xticks = np.arange(0, 550, 100) |
|
147 | 148 | f = np.array([150, 140]).reshape(-1, 1) |
148 | 149 | xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * prng.randn(*t.shape) |
149 | 150 |
|
150 | | -fig, (ax0, ax1) = plt.subplots(ncols=2) |
| 151 | +fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True) |
151 | 152 |
|
152 | | -fig.subplots_adjust(hspace=0.45, wspace=0.3) |
153 | 153 | yticks = np.arange(-50, 30, 10) |
154 | 154 | yrange = (yticks[0], yticks[-1]) |
155 | 155 | xticks = np.arange(-500, 550, 200) |
|
0 commit comments