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

Skip to content

Commit dc5b283

Browse files
committed
Small cleanups to QuiverKey.
- Text already calls FontProperties._from_any on anything passed as fontproperties; no need to do it ourselves again. - Group together x and y shifts for text positioning.
1 parent b31c5ae commit dc5b283

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

lib/matplotlib/quiver.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import numpy as np
2020
from numpy import ma
2121

22-
from matplotlib import _api, cbook, _docstring, font_manager
22+
from matplotlib import _api, cbook, _docstring
2323
import matplotlib.artist as martist
2424
import matplotlib.collections as mcollections
2525
from matplotlib.patches import CirclePolygon
@@ -303,14 +303,11 @@ def __init__(self, Q, X, Y, U, label,
303303
self.labelcolor = labelcolor
304304
self.fontproperties = fontproperties or dict()
305305
self.kw = kwargs
306-
_fp = self.fontproperties
307-
# boxprops = dict(facecolor='red')
308306
self.text = mtext.Text(
309-
text=label, # bbox=boxprops,
307+
text=label,
310308
horizontalalignment=self.halign[self.labelpos],
311309
verticalalignment=self.valign[self.labelpos],
312-
fontproperties=font_manager.FontProperties._from_any(_fp))
313-
310+
fontproperties=self.fontproperties)
314311
if self.labelcolor is not None:
315312
self.text.set_color(self.labelcolor)
316313
self._dpi_at_last_init = None
@@ -347,29 +344,20 @@ def _init(self):
347344
self.vector.set_figure(self.get_figure())
348345
self._dpi_at_last_init = self.Q.axes.figure.dpi
349346

350-
def _text_x(self, x):
351-
if self.labelpos == 'E':
352-
return x + self.labelsep
353-
elif self.labelpos == 'W':
354-
return x - self.labelsep
355-
else:
356-
return x
357-
358-
def _text_y(self, y):
359-
if self.labelpos == 'N':
360-
return y + self.labelsep
361-
elif self.labelpos == 'S':
362-
return y - self.labelsep
363-
else:
364-
return y
347+
def _text_shift(self):
348+
return {
349+
"N": (0, +self.labelsep),
350+
"S": (0, -self.labelsep),
351+
"E": (+self.labelsep, 0),
352+
"W": (-self.labelsep, 0),
353+
}[self.labelpos]
365354

366355
@martist.allow_rasterization
367356
def draw(self, renderer):
368357
self._init()
369358
self.vector.draw(renderer)
370-
x, y = self.get_transform().transform((self.X, self.Y))
371-
self.text.set_x(self._text_x(x))
372-
self.text.set_y(self._text_y(y))
359+
pos = self.get_transform().transform((self.X, self.Y))
360+
self.text.set_position(pos + self._text_shift())
373361
self.text.draw(renderer)
374362
self.stale = False
375363

0 commit comments

Comments
 (0)