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

Skip to content

Commit 72abc5d

Browse files
committed
Fix streamplot to work with colorbar.
Streamplot no longer returns an arrow patch and, instead, only returns a dummy LineCollection.
1 parent 6f3053e commit 72abc5d

5 files changed

Lines changed: 25 additions & 15 deletions

File tree

boilerplate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def %(func)s(%(argspec)s):
124124
#'spy' : 'sci(%(ret)s)', ### may return image or Line2D
125125
'quiver' : 'sci(%(ret)s)',
126126
'specgram' : 'sci(%(ret)s[-1])',
127-
'streamplot' : 'sci(%(ret)s[0])',
127+
'streamplot' : 'sci(%(ret)s)',
128128
'tricontour' : 'if %(ret)s._A is not None: sci(%(ret)s)',
129129
'tricontourf': 'if %(ret)s._A is not None: sci(%(ret)s)',
130130
'tripcolor' : 'sci(%(ret)s)',

examples/pylab_examples/streamplot_demo.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
V = 1 + X - Y**2
77
speed = np.sqrt(U*U + V*V)
88

9+
plt.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
10+
plt.colorbar()
11+
912
f, (ax1, ax2) = plt.subplots(ncols=2)
1013
ax1.streamplot(X, Y, U, V)
1114

1215
lw = 5*speed/speed.max()
13-
ax2.streamplot(X, Y, U, V, density=0.6,
14-
color=U, cmap=plt.cm.autumn, linewidth=lw)
16+
ax2.streamplot(X, Y, U, V, density=0.6, color=U, linewidth=lw)
1517

1618
plt.show()
1719

lib/matplotlib/axes.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6307,15 +6307,15 @@ def quiver(self, *args, **kw):
63076307
def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
63086308
cmap=None, arrowsize=1, arrowstyle='-|>', minlength=0.1):
63096309
if not self._hold: self.cla()
6310-
lines, arrows = mstream.streamplot(self, x, y, u, v,
6311-
density=density,
6312-
linewidth=linewidth,
6313-
color=color,
6314-
cmap=cmap,
6315-
arrowsize=arrowsize,
6316-
arrowstyle=arrowstyle,
6317-
minlength=minlength)
6318-
return lines, arrows
6310+
lines = mstream.streamplot(self, x, y, u, v,
6311+
density=density,
6312+
linewidth=linewidth,
6313+
color=color,
6314+
cmap=cmap,
6315+
arrowsize=arrowsize,
6316+
arrowstyle=arrowstyle,
6317+
minlength=minlength)
6318+
return lines
63196319
streamplot.__doc__ = mstream.streamplot.__doc__
63206320

63216321
@docstring.dedent_interpd

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2663,7 +2663,7 @@ def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None, arr
26632663
draw_if_interactive()
26642664
finally:
26652665
ax.hold(washold)
2666-
sci(ret[0])
2666+
sci(ret)
26672667
return ret
26682668

26692669
# This function was autogenerated by boilerplate.py. Do not edit as

lib/matplotlib/streamplot.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,18 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=1, color='k', cmap=None,
134134
p = mpp.FancyArrowPatch(arrow_tail, arrow_head, **arrow_kw)
135135
axes.add_patch(p)
136136

137+
# Add dummy line collection so that colorbar works correctly.
138+
if type(color) == np.ndarray:
139+
lc = matplotlib.collections.LineCollection([], **line_kw)
140+
lc.set_array(color.ravel())
141+
lc.set_cmap(cmap)
142+
lc.set_norm(norm)
143+
axes.add_collection(lc)
144+
137145
axes.update_datalim(((x.min(), y.min()), (x.max(), y.max())))
138146
axes.autoscale_view(tight=True)
139-
# TODO: Currently this returns only the last streamline and arrow patch.
140-
return lc, p
147+
# TODO: Currently this returns only dummy streamline
148+
return lc
141149

142150

143151
# Coordinate definitions

0 commit comments

Comments
 (0)