8000 API : tighten validation on pivot in Quiver · matplotlib/matplotlib@af17051 · GitHub
[go: up one dir, main page]

Skip to content

Commit af17051

Browse files
committed
API : tighten validation on pivot in Quiver
only accept {'mid', 'middle', 'tip', 'tail'} instead of being super permissive. Closes #3951
1 parent f0bb0ab commit af17051

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Tighted input validation on 'pivot' kwarg to quiver
2+
```````````````````````````````````````````````````
3+
4+
Tightened validation so that only {'tip', 'tail', 'mid', and 'middle'}
5+
(but any capitalization) are valid values for the 'pivot' kwarg in
6+
the `Quiver.__init__` (and hence `Axes.quiver` and
7+
`plt.quiver` which both fully delegate to `Quiver`). Previously any
8+
input matching 'mid.*' would be interpreted as 'middle', 'tip.*' as
9+
'tip' and any string not matching one of those patterns as 'tail'.
10+
11+
The value of `Quiver.pivot` is normalized to be in the set {'tip',
12+
'tail', 'middle'} in `Quiver.__init__`.

lib/matplotlib/quiver.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
is less than this, plot a dot (hexagon) of this diameter instead.
137137
Default is 1.
138138
139-
*pivot*: [ 'tail' | 'middle' | 'tip' ]
139+
*pivot*: [ 'tail' | 'mid' | 'middle' | 'tip' ]
140140
The part of the arrow that is at the grid point; the arrow rotates
141141
about this point, hence the name *pivot*.
142142
@@ -405,6 +405,8 @@ class Quiver(mcollections.PolyCollection):
405405
in the draw() method.
406406
"""
407407

408+
_PIVOT_VALS = ('tail', 'mid', 'middle', 'tip')
409+
408410
@docstring.Substitution(_quiver_doc)
409411
def __init__(self, ax, *args, **kw):
410412
"""
@@ -430,7 +432,18 @@ def __init__(self, ax, *args, **kw):
430432
self.angles = kw.pop('angles', 'uv')
431433
self.width = kw.pop('width', None)
432434
self.color = kw.pop('color', 'k')
433-
self.pivot = kw.pop('pivot', 'tail')
435+
436+
pivot = kw.pop('pivot', 'tail').lower()
437+
# validate pivot
438+
if pivot not in self._PIVOT_VALS:
439+
raise ValueError(
440+
'pivot must be one of {keys}, you passed {inp}'.format(
441+
keys=self._PIVOT_VALS, inp=pivot))
442+
# normalize to 'middle'
443+
if pivot == 'mid':
444+
pivot = 'middle'
445+
self.pivot = pivot
446+
434447
self.transform = kw.pop('transform', ax.transData)
435448
kw.setdefault('facecolors', self.color)
436449
kw.setdefault('linewidths', (0,))
@@ -681,9 +694,9 @@ def _h_arrows(self, length):
681694
# Now select X0, Y0 if short, otherwise X, Y
682695
cbook._putmask(X, short, X0)
683696
cbook._putmask(Y, short, Y0)
684-
if self.pivot[:3] == 'mid':
697+
if self.pivot == 'middle':
685698
X -= 0.5 * X[:, 3, np.newaxis]
686-
elif self.pivot[:3] == 'tip':
699+
elif self.pivot == 'tip':
687700
X = X - X[:, 3, np.newaxis] # numpy bug? using -= does not
688701
# work here unless we multiply
689702
# by a float first, as with 'mid'.

0 commit comments

Comments
 (0)
0