|
| 1 | +""" |
| 2 | +===================== |
| 3 | +Time Series Histogram |
| 4 | +===================== |
| 5 | +
|
| 6 | +This example demonstrates how to efficiently visualize large numbers of time |
| 7 | +series in a way that could potentially reveal hidden substructure and patterns |
| 8 | +that are not immediately obvious. |
| 9 | +
|
| 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 |
| 12 | +plots show how to reinterpret the data as a 2d histogram. |
| 13 | +""" |
| 14 | +from copy import copy |
| 15 | +import time |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import matplotlib.pyplot as plt |
| 19 | +from matplotlib.colors import LogNorm |
| 20 | + |
| 21 | +_, axes = plt.subplots(nrows=3, figsize=(10, 6 * 3)) |
| 22 | + |
| 23 | +# Make some data; lots of random noise + small fraction of sine waves |
| 24 | +num_series = 10000 |
| 25 | +num_points = 100 |
| 26 | +SNR = 0.05 # Signal to Noise Ratio |
| 27 | +x = np.linspace(0, 4 * np.pi, num_points) |
| 28 | +# random noise |
| 29 | +Y = np.random.randn(num_series, num_points) |
| 30 | +# sinusoidal signal |
| 31 | +num_signal = int(round(SNR * num_series)) |
| 32 | +phi = (0.25 * np.pi) * np.random.randn(num_signal, 1) |
| 33 | +Y[-num_signal:] = np.sin(x[None, :] - phi) + 0.1 * \ |
| 34 | + np.random.randn(num_signal, num_points) |
| 35 | + |
| 36 | +# Plot it using `plot` and the lowest nonzero value of alpha (1/256). |
| 37 | +# With this view it is extremely difficult to observe the sinusoidal behavior |
| 38 | +# because of how many overlapping series there are. It also takes some time |
| 39 | +# to run because so many individual plots that need to be generated. |
| 40 | +tic = time.time() |
| 41 | +for i in range(Y.shape[0]): |
| 42 | + axes[0].plot(x, Y[i], color="C0", alpha=1 / 256) |
| 43 | +toc = time.time() |
| 44 | +axes[0].set_title( |
| 45 | + r"Standard time series visualization using `plt.plot`") |
| 46 | +print(f"{toc-tic:.2f} sec. elapsed") # ~4 seconds |
| 47 | + |
| 48 | + |
| 49 | +# Now we will convert the multiple time series into a heat map. Not only will |
| 50 | +# the hidden signal be more visible, but it is also a much quicker procedure. |
| 51 | +tic = time.time() |
| 52 | +# linearly interpolate between the points in each time series |
| 53 | +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) |
| 61 | + |
| 62 | +# Plot (x, y) points in 2d histogram with log colorscale |
| 63 | +# It is pretty evident that there is some kind of structure under the noise |
| 64 | +# that has a periodicity of about ~6 and oscillates between +1/-1. |
| 65 | +cmap = copy(plt.cm.Blues) |
| 66 | +cmap.set_bad(cmap(0)) |
| 67 | +axes[1].hist2d(*xy.T, bins=[200, 200], cmap=cmap, norm=LogNorm()) |
| 68 | +axes[1].set_title( |
| 69 | + r"Alternative time series vis. using `plt.hist2d` and log color scale") |
| 70 | + |
| 71 | +# It is even visible on a linear color scale |
| 72 | +axes[2].hist2d(*xy.T, bins=[200, 200], cmap=cmap) |
| 73 | +axes[2].set_title( |
| 74 | + r"Alternative time series vis. using `plt.hist2d` and linear color scale") |
| 75 | +toc = time.time() |
| 76 | +print(f"{toc-tic:.2f} sec. elapsed") # ~1 sec for both plots |
| 77 | + |
| 78 | +plt.show() |
0 commit comments