Skip to content

Commit 4d2d6f7

Browse files
committed
reduced number of series for faster CI, modified alpha and vmax for better illustration, eliminated loop over each time series line plot, changed color map to plasma
1 parent 22a1392 commit 4d2d6f7

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

examples/statistics/time_series_histogram.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
1010
The first plot shows the typical way of visualizing multiple time series by
1111
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.
1314
"""
1415
from copy import copy
1516
import time
@@ -22,7 +23,7 @@
2223
_, axes = plt.subplots(nrows=3, figsize=(10, 6 * 3))
2324

2425
# Make some data; a 1D random walk + small fraction of sine waves
25-
num_series = 10000
26+
num_series = 1000
2627
num_points = 100
2728
SNR = 0.10 # Signal to Noise Ratio
2829
x = np.linspace(0, 4 * np.pi, num_points)
@@ -31,32 +32,31 @@
3132
# sinusoidal signal
3233
num_signal = int(round(SNR * num_series))
3334
phi = (np.pi / 8) * np.random.randn(num_signal, 1) # small random offest
34-
Y[-num_signal:] = ((
35+
Y[-num_signal:] = (
3536
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))
3839
)
3940

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.
4445
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)
4747
toc = time.time()
4848
axes[0].set_title(
4949
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
5151

5252

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
5454
# the hidden signal be more visible, but it is also a much quicker procedure.
5555
tic = time.time()
5656
# linearly interpolate between the points in each time series
57-
num_fine = 400 * 3
57+
num_fine = 800
5858
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)
6060
for i in range(num_series):
6161
y_fine[i, :] = np.interp(x_fine, x, Y[i, :])
6262
y_fine = y_fine.flatten()
@@ -65,22 +65,21 @@
6565

6666
# Plot (x, y) points in 2d histogram with log colorscale
6767
# 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)
6970
cmap.set_bad(cmap(0))
7071
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))
7273
axes[1].set_title(
7374
r"Alternative time series vis. using 2d histogram and log color scale")
7475

7576
# Same thing on linear color scale but with different (more visible) cmap
76-
cmap = copy(plt.cm.Blues)
77-
cmap.set_bad(cmap(0))
7877
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)
8179
axes[2].set_title(
8280
r"Alternative time series vis. using 2d histogram and linear color scale")
8381
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")
8584

8685
plt.show()

0 commit comments

Comments
 (0)