8000 Merge pull request #14434 from meeseeksmachine/auto-backport-of-pr-14… · matplotlib/matplotlib@dfc8dbd · GitHub
[go: up one dir, main page]

Skip to content

Commit dfc8dbd

Browse files
authored
Merge pull request #14434 from meeseeksmachine/auto-backport-of-pr-14296-on-v3.1.x
Backport PR #14296 on branch v3.1.x (Fix barbs to accept array of bool for `flip_barb`)
2 parents ce3ccae + 90a20b0 commit dfc8dbd

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

lib/matplotlib/quiver.py

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

1086-
# Check for flip
1087-
if flip:
1088-
full_height = -full_height
1089-
10901086
endx = 0.0
10911087
try:
10921088
endy = float(pivot)
@@ -1124,6 +1120,9 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11241120
poly_verts = [(endx, endy)]
11251121
offset = length
11261122

1123+
# Handle if this barb should be flipped
1124+
barb_height = -full_height if flip[index] else full_height
1125+
11271126
# Add vertices for each flag
11281127
for i in range(nflags[index]):
11291128
# The spacing that works for the barbs is a little to much for
@@ -1133,7 +1132,7 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11331132
offset += spacing / 2.
11341133
poly_verts.extend(
11351134
[[endx, endy + offset],
1136-
[endx + full_height, endy - full_width / 2 + offset],
1135+
[endx + barb_height, endy - full_width / 2 + offset],
11371136
[endx, endy - full_width + offset]])
11381137

11391138
offset -= full_width + spacing
@@ -1144,7 +1143,7 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11441143
for i in range(nbarbs[index]):
11451144
poly_verts.extend(
11461145
[(endx, endy + offset),
1147-
(endx + full_height, endy + offset + full_width / 2),
1146+
(endx + barb_height, endy + offset + full_width / 2),
11481147
(endx, endy + offset)])
11491148

11501149
offset -= spacing
@@ -1159,7 +1158,7 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11591158
offset -= 1.5 * spacing
11601159
poly_verts.extend(
11611160
[(endx, endy + offset),
1162-
(endx + full_height / 2, endy + offset + full_width / 4),
1161+
(endx + barb_height / 2, endy + offset + full_width / 4),
11631162
(endx, endy + offset)])
11641163

11651164
# Rotate the barb according the angle. Making the barb first and
@@ -1174,15 +1173,25 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11741173
def set_UVC(self, U, V, C=None):
11751174
self.u = ma.masked_invalid(U, copy=False).ravel()
11761175
self.v = ma.masked_invalid(V, copy=False).ravel()
1176+
1177+
# Flip needs to have the same number of entries as everything else.
1178+
# Use broadcast_to to avoid a bloated array of identical values.
1179+
# (can't rely on actual broadcasting)
1180+
if len(self.flip) == 1:
1181+
flip = np.broadcast_to(self.flip, self.u.shape)
1182+
else:
1183+
flip = self.flip
1184+
11771185
if C is not None:
11781186
c = ma.masked_invalid(C, copy=False).ravel()
1179-
x, y, u, v, c = cbook.delete_masked_points(
1180-
self.x.ravel(), self.y.ravel(), self.u, self.v, c)
1181-
_check_consistent_shapes(x, y, u, v, c)
1187+
x, y, u, v, c, flip = cbook.delete_masked_points(
1188+
self.x.ravel(), self.y.ravel(), self.u, self.v, c,
1189+
flip.ravel())
1190+
_check_consistent_shapes(x, y, u, v, c, flip)
11821191
else:
1183-
x, y, u, v = cbook.delete_masked_points(
1184-
self.x.ravel(), self.y.ravel(), self.u, self.v)
1185-
_check_consistent_shapes(x, y, u, v)
1192+
x, y, u, v, flip = cbook.delete_masked_points(
1193+
self.x.ravel(), self.y.ravel(), self.u, self.v, flip.ravel())
1194+
_check_consistent_shapes(x, y, u, v, flip)
11861195

11871196
magnitude = np.hypot(u, v)
11881197
flags, barbs, halves, empty = self._find_tails(magnitude,
@@ -1193,7 +1202,7 @@ def set_UVC(self, U, V, C=None):
11931202

11941203
plot_barbs = self._make_barbs(u, v, flags, barbs, halves, empty,
11951204
self._length, self._pivot, self.sizes,
1196-
self.fill_empty, self.flip)
1205+
self.fill_empty, flip)
11971206
self.set_verts(plot_barbs)
11981207

11991208
# Set the color array
Loading

lib/matplotlib/tests/test_quiver.py

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

180180

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

0 commit comments

Comments
 (0)
0