|
23 | 23 | histogram, with optional interpolation between data points, by using |
24 | 24 | ``np.histogram2d`` and ``plt.pcolormesh``. |
25 | 25 | """ |
26 | | -from copy import copy |
| 26 | + |
27 | 27 | import time |
28 | 28 |
|
29 | 29 | import matplotlib.pyplot as plt |
30 | 30 | import numpy as np |
31 | | -import numpy.matlib |
32 | | - |
33 | | -from matplotlib.colors import LogNorm |
34 | 31 |
|
35 | 32 | fig, axes = plt.subplots(nrows=3, figsize=(6, 8), layout='constrained') |
36 | 33 |
|
| 34 | +# Fix random state for reproducibility |
| 35 | +np.random.seed(19680801) |
37 | 36 | # Make some data; a 1D random walk + small fraction of sine waves |
38 | 37 | num_series = 1000 |
39 | 38 | num_points = 100 |
|
45 | 44 | num_signal = round(SNR * num_series) |
46 | 45 | phi = (np.pi / 8) * np.random.randn(num_signal, 1) # small random offset |
47 | 46 | Y[-num_signal:] = ( |
48 | | - np.sqrt(np.arange(num_points))[None, :] # random walk RMS scaling factor |
49 | | - * (np.sin(x[None, :] - phi) |
| 47 | + np.sqrt(np.arange(num_points)) # random walk RMS scaling factor |
| 48 | + * (np.sin(x - phi) |
50 | 49 | + 0.05 * np.random.randn(num_signal, num_points)) # small random noise |
51 | 50 | ) |
52 | 51 |
|
|
68 | 67 | # Linearly interpolate between the points in each time series |
69 | 68 | num_fine = 800 |
70 | 69 | x_fine = np.linspace(x.min(), x.max(), num_fine) |
71 | | -y_fine = np.empty((num_series, num_fine), dtype=float) |
72 | | -for i in range(num_series): |
73 | | - y_fine[i, :] = np.interp(x_fine, x, Y[i, :]) |
74 | | -y_fine = y_fine.flatten() |
75 | | -x_fine = np.matlib.repmat(x_fine, num_series, 1).flatten() |
| 70 | +y_fine = np.concatenate([np.interp(x_fine, x, y_row) for y_row in Y]) |
| 71 | +x_fine = np.broadcast_to(x_fine, (num_series, num_fine)).ravel() |
76 | 72 |
|
77 | 73 |
|
78 | 74 | # Plot (x, y) points in 2d histogram with log colorscale |
79 | 75 | # It is pretty evident that there is some kind of structure under the noise |
80 | 76 | # You can tune vmax to make signal more visible |
81 | | -cmap = copy(plt.cm.plasma) |
82 | | -cmap.set_bad(cmap(0)) |
| 77 | +cmap = plt.colormaps["plasma"] |
| 78 | +cmap = cmap.with_extremes(bad=cmap(0)) |
83 | 79 | h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[400, 100]) |
84 | 80 | pcm = axes[1].pcolormesh(xedges, yedges, h.T, cmap=cmap, |
85 | | - norm=LogNorm(vmax=1.5e2), rasterized=True) |
| 81 | + norm="log", vmax=1.5e2, rasterized=True) |
86 | 82 | fig.colorbar(pcm, ax=axes[1], label="# points", pad=0) |
87 | 83 | axes[1].set_title("2d histogram and log color scale") |
88 | 84 |
|
|
0 commit comments