8000 Return collection of streamlines from `streamplot`. · matplotlib/matplotlib@3259d0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3259d0a

Browse files
committed
Return collection of streamlines from streamplot.
Previously, `streamplot` just returned a dummy LineCollection so that colorbars and legends were properly set. Bonus: moving the creation of LineCollection objects out of the loop boosted the speed by about 10%.
1 parent 07553f0 commit 3259d0a

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

lib/matplotlib/streamplot.py

Lines changed: 25 additions & 15 deletions
45
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=1, color='k', cmap=None,
4141
Arrow style specification. See `matplotlib.patches.FancyArrowPatch`.
4242
minlength : float
4343
Minimum length of streamline in axes coordinates.
44+
+
Returns
46+
-------
47+
streamlines : `matplotlib.collections.LineCollection`
48+
Line collection with all streamlines as a series of line segments.
49+
Currently, there is no way to differentiate between line segments
50+
on different streamlines (other than manually checking that segments
51+
are connected).
4452
"""
4553
grid = Grid(x, y)
4654
mask = StreamMask(density)
@@ -84,14 +92,19 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=1, color='k', cmap=None,
8492
line_kw = {}
8593
arrow_kw = dict(arrowstyle=arrowstyle, mutation_scale=10*arrowsize)
8694

87-
if not type(linewidth) == np.ndarray:
95+
if type(linewidth) == np.ndarray:
96+
line_kw['linewidth'] = []
97+
else:
8898
line_kw['linewidth'] = linewidth
8999
arrow_kw['linewidth'] = linewidth
90100

91-
if not type(color) == np.ndarray:
101+
if type(color) == np.ndarray:
102+
line_colors = []
103+
else:
92104
line_kw['color'] = color
93105
arrow_kw['color'] = color
94106

107+
streamlines = []
95108
for t in trajectories:
96109
tgx = np.array(t[0])
97110
tgy = np.array(t[1])
@@ -100,7 +113,7 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=1, color='k', cmap=None,
100113
ty = np.array(t[1]) * grid.dy + grid.y_origin
101114

102115
points = np.transpose([tx, ty]).reshape(-1, 1, 2)
103-
segments = np.concatenate([points[:-1], points[1:]], axis=1)
116+
streamlines.extend(np.hstack([points[:-1], points[1:]]))
104117

105118
## Add arrows half way along each trajectory.
106119
s = np.cumsum(np.sqrt(np.diff(tx)**2 + np.diff(ty)**2))
@@ -109,30 +122,27 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=1, color='k', cmap=None,
109122
arrow_head = (np.mean(tx[n:n+2]), np.mean(ty[n:n+2]))
110123

111124
if type(linewidth) == np.ndarray:
112-
line_kw['linewidth'] = interpgrid(linewidth, tgx, tgy)[:-1]
113-
arrow_kw['linewidth'] = line_kw['linewidth'][n]
125+
line_widths = interpgrid(linewidth, tgx, tgy)[:-1]
126+
line_kw['linewidth'].extend(line_widths)
127+
arrow_kw['linewidth'] = line_widths[n]
114128

115129
if type(color) == np.ndarray:
116-
line_kw['color'] = cmap(norm(interpgrid(color, tgx, tgy)[:-1]))
117-
arrow_kw['color'] = line_kw['color'][n]
118-
119-
lc = matplotlib.collections.LineCollection(segments, **line_kw)
120-
axes.add_collection(lc)
130+
color_values = interpgrid(color, tgx, tgy)[:-1]
131+
line_colors.extend(color_values)
132+
arrow_kw['color'] = cmap(norm(color_values[n]))
121133

122134
p = mpp.FancyArrowPatch(arrow_tail, arrow_head, **arrow_kw)
123135
axes.add_patch(p)
124136

125-
# Add dummy line collection so that colorbar works correctly.
137+
lc = matplotlib.collections.LineCollection(streamlines, **line_kw)
126138
if type(color) == np.ndarray:
127-
lc = matplotlib.collections.LineCollection([], **line_kw)
128-
lc.set_array(color.ravel())
139+
lc.set_array(np.asarray(line_colors))
129140
lc.set_cmap(cmap)
130141
lc.set_norm(norm)
131-
axes.add_collection(lc)
142+
axes.add_collection(lc)
132143

133144
axes.update_datalim(((x.min(), y.min()), (x.max(), y.max())))
134145
axes.autoscale_view(tight=True)
135-
# TODO: Currently this returns only dummy streamline
136146
return lc
137147

138148

0 commit comments

Comments
 (0)
0