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

Skip to content

Commit 7eb563c

Browse files
committed
fix docs and split xy into separate vectors
1 parent bf47ef2 commit 7eb563c

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

examples/statistics/time_series_histogram.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
that are not immediately obvious.
99
1010
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
11+
overlaying them on top of each other with ``plt.plot``. The second and third
1212
plots show how to reinterpret the data as a 2d histogram.
1313
"""
1414
from copy import copy
1515
import time
1616

1717
import numpy as np
18+
import numpy.matlib
1819
import matplotlib.pyplot as plt
1920
from matplotlib.colors import LogNorm
2021

@@ -51,30 +52,30 @@
5152
tic = time.time()
5253
# linearly interpolate between the points in each time series
5354
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)
55+
x_fine = np.linspace(x.min(), x.max(), num_fine)
56+
y_fine = np.zeros((num_series, num_fine))
57+
for i in range(num_series):
58+
y_fine[i, :] = np.interp(x_fine, x, Y[i, :])
59+
y_fine = y_fine.flatten()
60+
x_fine = np.matlib.repmat(x_fine, num_series, 1).flatten()
61+
6162

6263
# Plot (x, y) points in 2d histogram with log colorscale
6364
# It is pretty evident that there is some kind of structure under the noise
6465
# that has a periodicity of about ~6 and oscillates between +1/-1.
6566
cmap = copy(plt.cm.Blues)
6667
cmap.set_bad(cmap(0))
67-
h, xedges, yedges = np.histogram2d(*xy.T, bins=[200, 200])
68+
h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[200, 200])
6869
axes[1].pcolormesh(xedges, yedges, h.T, cmap=cmap, norm=LogNorm())
6970
axes[1].set_title(
7071
r"Alternative time series vis. using `plt.hist2d` and log color scale")
7172

7273
# It is even visible on a linear color scale
73-
h, xedges, yedges = np.histogram2d(*xy.T, bins=[200, 200])
74+
h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[200, 200])
7475
axes[2].pcolormesh(xedges, yedges, h.T, cmap=cmap)
7576
axes[2].set_title(
7677
r"Alternative time series vis. using `plt.hist2d` and linear color scale")
7778
toc = time.time()
78-
print(f"{toc-tic:.2f} sec. elapsed") # ~1 sec for both plots
79+
print(f"{toc-tic:.2f} sec. elapsed") # ~1 sec for both plots + interpolation
7980

8081
plt.show()

0 commit comments

Comments
 (0)