|
8 | 8 | that are not immediately obvious. |
9 | 9 |
|
10 | 10 | The first plot shows the typical way of visualizing multiple time series by |
11 | | -overlaying them on top of each other with `plt.plot`. The second and third |
| 11 | +overlaying them on top of each other with ``plt.plot``. The second and third |
12 | 12 | plots show how to reinterpret the data as a 2d histogram. |
13 | 13 | """ |
14 | 14 | from copy import copy |
15 | 15 | import time |
16 | 16 |
|
17 | 17 | import numpy as np |
| 18 | +import numpy.matlib |
18 | 19 | import matplotlib.pyplot as plt |
19 | 20 | from matplotlib.colors import LogNorm |
20 | 21 |
|
|
51 | 52 | tic = time.time() |
52 | 53 | # linearly interpolate between the points in each time series |
53 | 54 | num_fine = 1000 |
54 | | -x_fine = np.linspace(x.min(), x.max(), num_fine) # x_fine.shape == (1_000,) |
55 | | -y_fine = np.stack([np.interp(x_fine, x, Y[i]) for i in range( |
56 | | - Y.shape[0])], axis=0) # y_fine.shape = (10_000, 1_000) |
57 | | -# convert into tensor of (x, y) pairs along the -1 axis |
58 | | -xy = np.stack([np.broadcast_to(x_fine[None, :], y_fine.shape), |
59 | | - y_fine], axis=-1) # xy.shape == (10_000, 1_000, 2) |
60 | | -xy = xy.reshape(-1, 2) # xy.shape = (10_000_000, 2) |
| 55 | +x_fine = np.linspace(x.min(), x.max(), num_fine) |
| 56 | +y_fine = np.zeros((num_series, num_fine)) |
| 57 | +for i in range(num_series): |
| 58 | + y_fine[i, :] = np.interp(x_fine, x, Y[i, :]) |
| 59 | +y_fine = y_fine.flatten() |
| 60 | +x_fine = np.matlib.repmat(x_fine, num_series, 1).flatten() |
| 61 | + |
61 | 62 |
|
62 | 63 | # Plot (x, y) points in 2d histogram with log colorscale |
63 | 64 | # It is pretty evident that there is some kind of structure under the noise |
64 | 65 | # that has a periodicity of about ~6 and oscillates between +1/-1. |
65 | 66 | cmap = copy(plt.cm.Blues) |
66 | 67 | cmap.set_bad(cmap(0)) |
67 | | -h, xedges, yedges = np.histogram2d(*xy.T, bins=[200, 200]) |
| 68 | +h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[200, 200]) |
68 | 69 | axes[1].pcolormesh(xedges, yedges, h.T, cmap=cmap, norm=LogNorm()) |
69 | 70 | axes[1].set_title( |
70 | 71 | r"Alternative time series vis. using `plt.hist2d` and log color scale") |
71 | 72 |
|
72 | 73 | # It is even visible on a linear color scale |
73 | | -h, xedges, yedges = np.histogram2d(*xy.T, bins=[200, 200]) |
| 74 | +h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[200, 200]) |
74 | 75 | axes[2].pcolormesh(xedges, yedges, h.T, cmap=cmap) |
75 | 76 | axes[2].set_title( |
76 | 77 | r"Alternative time series vis. using `plt.hist2d` and linear color scale") |
77 | 78 | toc = time.time() |
78 | | -print(f"{toc-tic:.2f} sec. elapsed") # ~1 sec for both plots |
| 79 | +print(f"{toc-tic:.2f} sec. elapsed") # ~1 sec for both plots + interpolation |
79 | 80 |
|
80 | 81 | plt.show() |
0 commit comments