Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 818636e

Browse files
committed
add more detail to intro/comments, remove superfluous comments/code, shorten titles
1 parent 4d2d6f7 commit 818636e

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

examples/statistics/time_series_histogram.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
66
This example demonstrates how to efficiently visualize large numbers of time
77
series in a way that could potentially reveal hidden substructure and patterns
8-
that are not immediately obvious.
8+
that are not immediately obvious, and display them in a visually appealing way.
9+
10+
In this example, we generate multiple sinusoidal "signal" series that are
11+
buried under a larger number of random walk "noise/background" series. For an
12+
unbiased Gaussian random walk with standard deviation of σ, the RMS deviation
13+
from the origin after n steps is σ*sqrt(n). So in order to keep the sinusoids
14+
visible on the same scale as the random walks, we scale the amplitude by the
15+
random walk RMS. In addition, we also introduce a small random offset ``phi``
16+
to shift the sines left/right, and some additive random noise to shift
17+
individual data points up/down to make the signal a bit more "realistic" (you
18+
wouldn't expect a perfect sine wave to appear in your data).
919
1020
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, with optional
13-
interpolation.
21+
overlaying them on top of each other with ``plt.plot`` and a small value of
22+
``alpha``. The second and third plots show how to reinterpret the data as a 2d
23+
histogram, with optional interpolation between data points, by using
24+
``np.histogram2d`` and ``plt.pcolormesh``.
1425
"""
1526
from copy import copy
1627
import time
@@ -27,33 +38,33 @@
2738
num_points = 100
2839
SNR = 0.10 # Signal to Noise Ratio
2940
x = np.linspace(0, 4 * np.pi, num_points)
30-
# random walk
41+
# Generate unbiased Gaussian random walks
3142
Y = np.cumsum(np.random.randn(num_series, num_points), axis=-1)
32-
# sinusoidal signal
43+
# Generate sinusoidal signals
3344
num_signal = int(round(SNR * num_series))
3445
phi = (np.pi / 8) * np.random.randn(num_signal, 1) # small random offest
3546
Y[-num_signal:] = (
36-
np.sqrt(np.arange(num_points))[None, :]
47+
np.sqrt(np.arange(num_points))[None, :] # random walk RMS scaling factor
3748
* (np.sin(x[None, :] - phi)
38-
+ 0.05 * np.random.randn(num_signal, num_points))
49+
+ 0.05 * np.random.randn(num_signal, num_points)) # small random noise
3950
)
4051

41-
# Plot it using `plot` and a small value of alpha. With this view it is
52+
53+
# Plot series using `plot` and a small value of `alpha`. With this view it is
4254
# very difficult to observe the sinusoidal behavior because of how many
4355
# overlapping series there are. It also takes a bit of time to run because so
44-
# many individual artists that need to be generated.
56+
# many individual artists need to be generated.
4557
tic = time.time()
4658
axes[0].plot(x, Y.T, color="C0", alpha=0.1)
4759
toc = time.time()
48-
axes[0].set_title(
49-
r"Standard time series visualization using line plot")
50-
print(f"{toc-tic:.2f} sec. elapsed") # ~0.26 seconds
60+
axes[0].set_title("Line plot with alpha")
61+
print(f"{toc-tic:.2f} sec. elapsed")
5162

5263

5364
# Now we will convert the multiple time series into a histogram. Not only will
5465
# the hidden signal be more visible, but it is also a much quicker procedure.
5566
tic = time.time()
56-
# linearly interpolate between the points in each time series
67+
# Linearly interpolate between the points in each time series
5768
num_fine = 800
5869
x_fine = np.linspace(x.min(), x.max(), num_fine)
5970
y_fine = np.empty((num_series, num_fine), dtype=float)
@@ -70,16 +81,12 @@
7081
cmap.set_bad(cmap(0))
7182
h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[400, 100])
7283
axes[1].pcolormesh(xedges, yedges, h.T, cmap=cmap, norm=LogNorm(vmax=1.5e2))
73-
axes[1].set_title(
74-
r"Alternative time series vis. using 2d histogram and log color scale")
84+
axes[1].set_title("2d histogram and log color scale")
7585

76-
# Same thing on linear color scale but with different (more visible) cmap
77-
h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[400, 100])
86+
# Same data but on linear color scale
7887
axes[2].pcolormesh(xedges, yedges, h.T, cmap=cmap, vmax=1.5e2)
79-
axes[2].set_title(
80-
r"Alternative time series vis. using 2d histogram and linear color scale")
88+
axes[2].set_title("2d histogram and linear color scale")
8189
toc = time.time()
82-
# ~0.08 sec for both plots + interpolation
8390
print(f"{toc-tic:.2f} sec. elapsed")
8491

8592
plt.show()

0 commit comments

Comments
 (0)