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

Skip to content

Commit 2ad9ec6

Browse files
authored
Merge pull request #20739 from anntzer/streamclean
Small simplifications to streamplot.
2 parents 56b653e + b0bab4b commit 2ad9ec6

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``matplotlib.streamplot.get_integrator``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated.

lib/matplotlib/streamplot.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
140140
u = np.ma.masked_invalid(u)
141141
v = np.ma.masked_invalid(v)
142142

143-
integrate = get_integrator(u, v, dmap, minlength, maxlength,
144-
integration_direction)
143+
integrate = _get_integrator(u, v, dmap, minlength, maxlength,
144+
integration_direction)
145145

146146
trajectories = []
147147
if start_points is None:
@@ -176,18 +176,14 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
176176
if use_multicolor_lines:
177177
if norm is None:
178178
norm = mcolors.Normalize(color.min(), color.max())
179-
if cmap is None:
180-
cmap = cm.get_cmap(matplotlib.rcParams['image.cmap'])
181-
else:
182-
cmap = cm.get_cmap(cmap)
179+
cmap = cm.get_cmap(cmap)
183180

184181
streamlines = []
185182
arrows = []
186183
for t in trajectories:
187-
tgx = np.array(t[0])
188-
tgy = np.array(t[1])
184+
tgx, tgy = t.T
189185
# Rescale from grid-coordinates to data-coordinates.
190-
tx, ty = dmap.grid2data(*np.array(t))
186+
tx, ty = dmap.grid2data(tgx, tgy)
191187
tx += grid.x_origin
192188
ty += grid.y_origin
193189

@@ -427,7 +423,7 @@ class TerminateTrajectory(Exception):
427423
# Integrator definitions
428424
# =======================
429425

430-
def get_integrator(u, v, dmap, minlength, maxlength, integration_direction):
426+
def _get_integrator(u, v, dmap, minlength, maxlength, integration_direction):
431427

432428
# rescale velocity onto grid-coordinates for integrations.
433429
u, v = dmap.data2grid(u, v)
@@ -454,7 +450,7 @@ def backward_time(xi, yi):
454450

455451
def integrate(x0, y0):
456452
"""
457-
Return x, y grid-coordinates of trajectory based on starting point.
453+
Return (N, 2) grid-coordinates of trajectory based on starting point.
458454
459455
Integrate both forward and backward in time from starting point in
460456
grid coordinates.
@@ -464,37 +460,41 @@ def integrate(x0, y0):
464460
resulting trajectory is None if it is shorter than `minlength`.
465461
"""
466462

467-
stotal, x_traj, y_traj = 0., [], []
463+
stotal, xy_traj = 0., []
468464

469465
try:
470466
dmap.start_trajectory(x0, y0)
471467
except InvalidIndexError:
472468
return None
473469
if integration_direction in ['both', 'backward']:
474-
s, xt, yt = _integrate_rk12(x0, y0, dmap, backward_time, maxlength)
470+
s, xyt = _integrate_rk12(x0, y0, dmap, backward_time, maxlength)
475471
stotal += s
476-
x_traj += xt[::-1]
477-
y_traj += yt[::-1]
472+
xy_traj += xyt[::-1]
478473

479474
if integration_direction in ['both', 'forward']:
480475
dmap.reset_start_point(x0, y0)
481-
s, xt, yt = _integrate_rk12(x0, y0, dmap, forward_time, maxlength)
482-
if len(x_traj) > 0:
483-
xt = xt[1:]
484-
yt = yt[1:]
476+
s, xyt = _integrate_rk12(x0, y0, dmap, forward_time, maxlength)
485477
stotal += s
486-
x_traj += xt
487-
y_traj += yt
478+
xy_traj += xyt[1:]
488479

489480
if stotal > minlength:
490-
return x_traj, y_traj
481+
return np.broadcast_arrays(xy_traj, np.empty((1, 2)))[0]
491482
else: # reject short trajectories
492483
dmap.undo_trajectory()
493484
return None
494485

495486
return integrate
496487

497488

489+
@_api.deprecated("3.5")
490+
def get_integrator(u, v, dmap, minlength, maxlength, integration_direction):
491+
xy_traj = _get_integrator(
492+
u, v, dmap, minlength, maxlength, integration_direction)
493+
return (None if xy_traj is None
494+
else ([], []) if not len(xy_traj)
495+
else [*zip(*xy_traj)])
496+
497+
498498
class OutOfBounds(IndexError):
499499
pass
500500

@@ -539,14 +539,12 @@ def _integrate_rk12(x0, y0, dmap, f, maxlength):
539539
stotal = 0
540540
xi = x0
541541
yi = y0
542-
xf_traj = []
543-
yf_traj = []
542+
xyf_traj = []
544543

545544
while True:
546545
try:
547546
if dmap.grid.within_grid(xi, yi):
548-
xf_traj.append(xi)
549-
yf_traj.append(yi)
547+
xyf_traj.append((xi, yi))
550548
else:
551549
raise OutOfBounds
552550

@@ -560,9 +558,8 @@ def _integrate_rk12(x0, y0, dmap, f, maxlength):
560558
# Out of the domain during this step.
561559
# Take an Euler step to the boundary to improve neatness
562560
# unless the trajectory is currently empty.
563-
if xf_traj:
564-
ds, xf_traj, yf_traj = _euler_step(xf_traj, yf_traj,
565-
dmap, f)
561+
if xyf_traj:
562+
ds, xyf_traj = _euler_step(xyf_traj, dmap, f)
566563
stotal += ds
567564
break
568565
except TerminateTrajectory:
@@ -595,14 +592,13 @@ def _integrate_rk12(x0, y0, dmap, f, maxlength):
595592
else:
596593
ds = min(maxds, 0.85 * ds * (maxerror / error) ** 0.5)
597594

598-
return stotal, xf_traj, yf_traj
595+
return stotal, xyf_traj
599596

600597

601-
def _euler_step(xf_traj, yf_traj, dmap, f):
598+
def _euler_step(xyf_traj, dmap, f):
602599
"""Simple Euler integration step that extends streamline to boundary."""
603600
ny, nx = dmap.grid.shape
604-
xi = xf_traj[-1]
605-
yi = yf_traj[-1]
601+
xi, yi = xyf_traj[-1]
606602
cx, cy = f(xi, yi)
607603
if cx == 0:
608604
dsx = np.inf
@@ -617,9 +613,8 @@ def _euler_step(xf_traj, yf_traj, dmap, f):
617613
else:
618614
dsy = (ny - 1 - yi) / cy
619615
ds = min(dsx, dsy)
620-
xf_traj.append(xi + cx * ds)
621-
yf_traj.append(yi + cy * ds)
622-
return ds, xf_traj, yf_traj
616+
xyf_traj.append((xi + cx * ds, yi + cy * ds))
617+
return ds, xyf_traj
623618

624619

625620
# Utility functions

0 commit comments

Comments
 (0)