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

Skip to content

Commit 22a1392

Browse files
committed
change random noise to random walk, use jet cmap on log color scale plot
1 parent 7eb563c commit 22a1392

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

examples/statistics/time_series_histogram.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@
2121

2222
_, axes = plt.subplots(nrows=3, figsize=(10, 6 * 3))
2323

24-
# Make some data; lots of random noise + small fraction of sine waves
24+
# Make some data; a 1D random walk + small fraction of sine waves
2525
num_series = 10000
2626
num_points = 100
27-
SNR = 0.05 # Signal to Noise Ratio
27+
SNR = 0.10 # Signal to Noise Ratio
2828
x = np.linspace(0, 4 * np.pi, num_points)
29-
# random noise
30-
Y = np.random.randn(num_series, num_points)
29+
# random walk
30+
Y = np.cumsum(np.random.randn(num_series, num_points), axis=-1)
3131
# sinusoidal signal
3232
num_signal = int(round(SNR * num_series))
33-
phi = (0.25 * np.pi) * np.random.randn(num_signal, 1)
34-
Y[-num_signal:] = np.sin(x[None, :] - phi) + 0.1 * \
35-
np.random.randn(num_signal, num_points)
33+
phi = (np.pi / 8) * np.random.randn(num_signal, 1) # small random offest
34+
Y[-num_signal:] = ((
35+
np.sqrt(np.arange(num_points))[None, :]
36+
* np.sin(x[None, :] - phi))
37+
+ 0.05 * np.random.randn(num_signal, num_points)
38+
)
3639

3740
# Plot it using `plot` and the lowest nonzero value of alpha (1/256).
3841
# With this view it is extremely difficult to observe the sinusoidal behavior
@@ -43,15 +46,15 @@
4346
axes[0].plot(x, Y[i], color="C0", alpha=1 / 256)
4447
toc = time.time()
4548
axes[0].set_title(
46-
r"Standard time series visualization using `plt.plot`")
49+
r"Standard time series visualization using line plot")
4750
print(f"{toc-tic:.2f} sec. elapsed") # ~4 seconds
4851

4952

5053
# Now we will convert the multiple time series into a heat map. Not only will
5154
# the hidden signal be more visible, but it is also a much quicker procedure.
5255
tic = time.time()
5356
# linearly interpolate between the points in each time series
54-
num_fine = 1000
57+
num_fine = 400 * 3
5558
x_fine = np.linspace(x.min(), x.max(), num_fine)
5659
y_fine = np.zeros((num_series, num_fine))
5760
for i in range(num_series):
@@ -62,19 +65,21 @@
6265

6366
# Plot (x, y) points in 2d histogram with log colorscale
6467
# It is pretty evident that there is some kind of structure under the noise
65-
# that has a periodicity of about ~6 and oscillates between +1/-1.
66-
cmap = copy(plt.cm.Blues)
68+
cmap = copy(plt.cm.jet)
6769
cmap.set_bad(cmap(0))
68-
h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[200, 200])
70+
h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[400, 100])
6971
axes[1].pcolormesh(xedges, yedges, h.T, cmap=cmap, norm=LogNorm())
7072
axes[1].set_title(
71-
r"Alternative time series vis. using `plt.hist2d` and log color scale")
73+
r"Alternative time series vis. using 2d histogram and log color scale")
7274

73-
# It is even visible on a linear color scale
74-
h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[200, 200])
75-
axes[2].pcolormesh(xedges, yedges, h.T, cmap=cmap)
75+
# 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+
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)
7681
axes[2].set_title(
77-
r"Alternative time series vis. using `plt.hist2d` and linear color scale")
82+
r"Alternative time series vis. using 2d histogram and linear color scale")
7883
toc = time.time()
7984
print(f"{toc-tic:.2f} sec. elapsed") # ~1 sec for both plots + interpolation
8085

0 commit comments

Comments
 (0)