|
9 | 9 |
|
10 | 10 | The first plot shows the typical way of visualizing multiple time series by |
11 | 11 | overlaying them on top of each other with ``plt.plot``. The second and third |
12 | | -plots show how to reinterpret the data as a 2d histogram. |
| 12 | +plots show how to reinterpret the data as a 2d histogram, with optional |
| 13 | +interpolation. |
13 | 14 | """ |
14 | 15 | from copy import copy |
15 | 16 | import time |
|
22 | 23 | _, axes = plt.subplots(nrows=3, figsize=(10, 6 * 3)) |
23 | 24 |
|
24 | 25 | # Make some data; a 1D random walk + small fraction of sine waves |
25 | | -num_series = 10000 |
| 26 | +num_series = 1000 |
26 | 27 | num_points = 100 |
27 | 28 | SNR = 0.10 # Signal to Noise Ratio |
28 | 29 | x = np.linspace(0, 4 * np.pi, num_points) |
|
31 | 32 | # sinusoidal signal |
32 | 33 | num_signal = int(round(SNR * num_series)) |
33 | 34 | phi = (np.pi / 8) * np.random.randn(num_signal, 1) # small random offest |
34 | | -Y[-num_signal:] = (( |
| 35 | +Y[-num_signal:] = ( |
35 | 36 | np.sqrt(np.arange(num_points))[None, :] |
36 | | - * np.sin(x[None, :] - phi)) |
37 | | - + 0.05 * np.random.randn(num_signal, num_points) |
| 37 | + * (np.sin(x[None, :] - phi) |
| 38 | + + 0.05 * np.random.randn(num_signal, num_points)) |
38 | 39 | ) |
39 | 40 |
|
40 | | -# Plot it using `plot` and the lowest nonzero value of alpha (1/256). |
41 | | -# With this view it is extremely difficult to observe the sinusoidal behavior |
42 | | -# because of how many overlapping series there are. It also takes some time |
43 | | -# to run because so many individual plots that need to be generated. |
| 41 | +# Plot it using `plot` and a small value of alpha. With this view it is |
| 42 | +# very difficult to observe the sinusoidal behavior because of how many |
| 43 | +# overlapping series there are. It also takes a bit of time to run because so |
| 44 | +# many individual artists that need to be generated. |
44 | 45 | tic = time.time() |
45 | | -for i in range(Y.shape[0]): |
46 | | - axes[0].plot(x, Y[i], color="C0", alpha=1 / 256) |
| 46 | +axes[0].plot(x, Y.T, color="C0", alpha=0.1) |
47 | 47 | toc = time.time() |
48 | 48 | axes[0].set_title( |
49 | 49 | r"Standard time series visualization using line plot") |
50 | | -print(f"{toc-tic:.2f} sec. elapsed") # ~4 seconds |
| 50 | +print(f"{toc-tic:.2f} sec. elapsed") # ~0.26 seconds |
51 | 51 |
|
52 | 52 |
|
53 | | -# Now we will convert the multiple time series into a heat map. Not only will |
| 53 | +# Now we will convert the multiple time series into a histogram. Not only will |
54 | 54 | # the hidden signal be more visible, but it is also a much quicker procedure. |
55 | 55 | tic = time.time() |
56 | 56 | # linearly interpolate between the points in each time series |
57 | | -num_fine = 400 * 3 |
| 57 | +num_fine = 800 |
58 | 58 | x_fine = np.linspace(x.min(), x.max(), num_fine) |
59 | | -y_fine = np.zeros((num_series, num_fine)) |
| 59 | +y_fine = np.empty((num_series, num_fine), dtype=float) |
60 | 60 | for i in range(num_series): |
61 | 61 | y_fine[i, :] = np.interp(x_fine, x, Y[i, :]) |
62 | 62 | y_fine = y_fine.flatten() |
|
65 | 65 |
|
66 | 66 | # Plot (x, y) points in 2d histogram with log colorscale |
67 | 67 | # It is pretty evident that there is some kind of structure under the noise |
68 | | -cmap = copy(plt.cm.jet) |
| 68 | +# You can tune vmax to make signal more visible |
| 69 | +cmap = copy(plt.cm.plasma) |
69 | 70 | cmap.set_bad(cmap(0)) |
70 | 71 | h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[400, 100]) |
71 | | -axes[1].pcolormesh(xedges, yedges, h.T, cmap=cmap, norm=LogNorm()) |
| 72 | +axes[1].pcolormesh(xedges, yedges, h.T, cmap=cmap, norm=LogNorm(vmax=1.5e2)) |
72 | 73 | axes[1].set_title( |
73 | 74 | r"Alternative time series vis. using 2d histogram and log color scale") |
74 | 75 |
|
75 | 76 | # Same thing on linear color scale but with different (more visible) cmap |
76 | | -cmap = copy(plt.cm.Blues) |
77 | | -cmap.set_bad(cmap(0)) |
78 | 77 | h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[400, 100]) |
79 | | -# tune vmax to make signal more visible |
80 | | -axes[2].pcolormesh(xedges, yedges, h.T, cmap=cmap, vmax=3e3) |
| 78 | +axes[2].pcolormesh(xedges, yedges, h.T, cmap=cmap, vmax=1.5e2) |
81 | 79 | axes[2].set_title( |
82 | 80 | r"Alternative time series vis. using 2d histogram and linear color scale") |
83 | 81 | toc = time.time() |
84 | | -print(f"{toc-tic:.2f} sec. elapsed") # ~1 sec for both plots + interpolation |
| 82 | +# ~0.08 sec for both plots + interpolation |
| 83 | +print(f"{toc-tic:.2f} sec. elapsed") |
85 | 84 |
|
86 | 85 | plt.show() |
0 commit comments