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

Skip to content

Commit e74800a

Browse files
authored
Merge pull request #14296 from dopplershift/fix-barb-flip
Fix barbs to accept array of bool for `flip_barb`
2 parents 2e44c2c + c87aea3 commit e74800a

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

lib/matplotlib/quiver.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ def __init__(self, ax, *args,
952952
self.fill_empty = fill_empty
953953
self.barb_increments = barb_increments or dict()
954954
self.rounding = rounding
955-
self.flip = flip_barb
955+
self.flip = np.atleast_1d(flip_barb)
956956
transform = kw.pop('transform', ax.transData)
957957
self._pivot = pivot
958958
self._length = length
@@ -1086,10 +1086,6 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
10861086
# Controls y point where to pivot the barb.
10871087
pivot_points = dict(tip=0.0, middle=-length / 2.)
10881088

1089-
# Check for flip
1090-
if flip:
1091-
full_height = -full_height
1092-
10931089
endx = 0.0
10941090
try:
10951091
endy = float(pivot)
@@ -1127,6 +1123,9 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11271123
poly_verts = [(endx, endy)]
11281124
offset = length
11291125

1126+
# Handle if this barb should be flipped
1127+
barb_height = -full_height if flip[index] else full_height
1128+
11301129
# Add vertices for each flag
11311130
for i in range(nflags[index]):
11321131
# The spacing that works for the barbs is a little to much for
@@ -1136,7 +1135,7 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11361135
offset += spacing / 2.
11371136
poly_verts.extend(
11381137
[[endx, endy + offset],
1139-
[endx + full_height, endy - full_width / 2 + offset],
1138+
[endx + barb_height, endy - full_width / 2 + offset],
11401139
[endx, endy - full_width + offset]])
11411140

11421141
offset -= full_width + spacing
@@ -1147,7 +1146,7 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11471146
for i in range(nbarbs[index]):
11481147
poly_verts.extend(
11491148
[(endx, endy + offset),
1150-
(endx + full_height, endy + offset + full_width / 2),
1149+
(endx + barb_height, endy + offset + full_width / 2),
11511150
(endx, endy + offset)])
11521151

11531152
offset -= spacing
@@ -1162,7 +1161,7 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11621161
offset -= 1.5 * spacing
11631162
poly_verts.extend(
11641163
[(endx, endy + offset),
1165-
(endx + full_height / 2, endy + offset + full_width / 4),
1164+
(endx + barb_height / 2, endy + offset + full_width / 4),
11661165
(endx, endy + offset)])
11671166

11681167
# Rotate the barb according the angle. Making the barb first and
@@ -1177,15 +1176,25 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11771176
def set_UVC(self, U, V, C=None):
11781177
self.u = ma.masked_invalid(U, copy=False).ravel()
11791178
self.v = ma.masked_invalid(V, copy=False).ravel()
1179+
1180+
# Flip needs to have the same number of entries as everything else.
1181+
# Use broadcast_to to avoid a bloated array of identical values.
1182+
# (can't rely on actual broadcasting)
1183+
if len(self.flip) == 1:
1184+
flip = np.broadcast_to(self.flip, self.u.shape)
1185+
else:
1186+
flip = self.flip
1187+
11801188
if C is not None:
11811189
c = ma.masked_invalid(C, copy=False).ravel()
1182-
x, y, u, v, c = cbook.delete_masked_points(
1183-
self.x.ravel(), self.y.ravel(), self.u, self.v, c)
1184-
_check_consistent_shapes(x, y, u, v, c)
1190+
x, y, u, v, c, flip = cbook.delete_masked_points(
1191+
self.x.ravel(), self.y.ravel(), self.u, self.v, c,
1192+
flip.ravel())
1193+
_check_consistent_shapes(x, y, u, v, c, flip)
11851194
else:
1186-
x, y, u, v = cbook.delete_masked_points(
1187-
self.x.ravel(), self.y.ravel(), self.u, self.v)
1188-
_check_consistent_shapes(x, y, u, v)
1195+
x, y, u, v, flip = cbook.delete_masked_points(
1196+
self.x.ravel(), self.y.ravel(), self.u, self.v, flip.ravel())
1197+
_check_consistent_shapes(x, y, u, v, flip)
11891198

11901199
magnitude = np.hypot(u, v)
11911200
flags, barbs, halves, empty = self._find_tails(magnitude,
@@ -1196,7 +1205,7 @@ def set_UVC(self, U, V, C=None):
11961205

11971206
plot_barbs = self._make_barbs(u, v, flags, barbs, halves, empty,
11981207
self._length, self._pivot, self.sizes,
1199-
self.fill_empty, self.flip)
1208+
self.fill_empty, flip)
12001209
self.set_verts(plot_barbs)
12011210

12021211
# Set the color array
18.3 KB
Loading

lib/matplotlib/tests/test_quiver.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ def test_barbs_pivot():
174174
ax.scatter(X, Y, s=49, c='black')
175175

176176

177+
@image_comparison(['barbs_test_flip.png'], remove_text=True)
178+
def test_barbs_flip():
179+
"""Test barbs with an array for flip_barb."""
180+
x = np.linspace(-5, 5, 5)
181+
X, Y = np.meshgrid(x, x)
182+
U, V = 12*X, 12*Y
183+
fig, ax = plt.subplots()
184+
ax.barbs(X, Y, U, V, fill_empty=True, rounding=False, pivot=1.7,
185+
sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3),
186+
flip_barb=Y < 0)
187+
188+
177189
def test_bad_masked_sizes():
178190
'Test error handling when given differing sized masked arrays'
179191
x = np.arange(3)

0 commit comments

Comments
 (0)