Skip to content

Commit b0c625a

Browse files
authored
Merge pull request #25939 from anntzer/tsh
Cleanup time_series_histogram example.
2 parents ffd3b12 + 4d0038f commit b0c625a

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

galleries/examples/statistics/time_series_histogram.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@
2323
histogram, with optional interpolation between data points, by using
2424
``np.histogram2d`` and ``plt.pcolormesh``.
2525
"""
26-
from copy import copy
26+
2727
import time
2828

2929
import matplotlib.pyplot as plt
3030
import numpy as np
31-
import numpy.matlib
32-
33-
from matplotlib.colors import LogNorm
3431

3532
fig, axes = plt.subplots(nrows=3, figsize=(6, 8), layout='constrained')
3633

34+
# Fix random state for reproducibility
35+
np.random.seed(19680801)
3736
# Make some data; a 1D random walk + small fraction of sine waves
3837
num_series = 1000
3938
num_points = 100
@@ -45,8 +44,8 @@
4544
num_signal = round(SNR * num_series)
4645
phi = (np.pi / 8) * np.random.randn(num_signal, 1) # small random offset
4746
Y[-num_signal:] = (
48-
np.sqrt(np.arange(num_points))[None, :] # random walk RMS scaling factor
49-
* (np.sin(x[None, :] - phi)
47+
np.sqrt(np.arange(num_points)) # random walk RMS scaling factor
48+
* (np.sin(x - phi)
5049
+ 0.05 * np.random.randn(num_signal, num_points)) # small random noise
5150
)
5251

@@ -68,21 +67,18 @@
6867
# Linearly interpolate between the points in each time series
6968
num_fine = 800
7069
x_fine = np.linspace(x.min(), x.max(), num_fine)
71-
y_fine = np.empty((num_series, num_fine), dtype=float)
72-
for i in range(num_series):
73-
y_fine[i, :] = np.interp(x_fine, x, Y[i, :])
74-
y_fine = y_fine.flatten()
75-
x_fine = np.matlib.repmat(x_fine, num_series, 1).flatten()
70+
y_fine = np.concatenate([np.interp(x_fine, x, y_row) for y_row in Y])
71+
x_fine = np.broadcast_to(x_fine, (num_series, num_fine)).ravel()
7672

7773

7874
# Plot (x, y) points in 2d histogram with log colorscale
7975
# It is pretty evident that there is some kind of structure under the noise
8076
# You can tune vmax to make signal more visible
81-
cmap = copy(plt.cm.plasma)
82-
cmap.set_bad(cmap(0))
77+
cmap = plt.colormaps["plasma"]
78+
cmap = cmap.with_extremes(bad=cmap(0))
8379
h, xedges, yedges = np.histogram2d(x_fine, y_fine, bins=[400, 100])
8480
pcm = axes[1].pcolormesh(xedges, yedges, h.T, cmap=cmap,
85-
norm=LogNorm(vmax=1.5e2), rasterized=True)
81+
norm="log", vmax=1.5e2, rasterized=True)
8682
fig.colorbar(pcm, ax=axes[1], label="# points", pad=0)
8783
axes[1].set_title("2d histogram and log color scale")
8884

0 commit comments

Comments
 (0)