Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

PEP8 fixes on text.py #1357

Merged
merged 2 commits into from

4 participants

@NelleV
Collaborator

PEP8 fixes on the module text

lib/matplotlib/text.py
@@ -16,18 +16,15 @@
from matplotlib.patches import bbox_artist, YAArrow, FancyBboxPatch, \
FancyArrowPatch, Rectangle
import matplotlib.transforms as mtransforms
-from matplotlib.transforms import Affine2D, Bbox, Transform ,\
- BboxBase, BboxTransformTo
+from matplotlib.transforms import Affine2D, Bbox, Transform,\
@dmcdougall Collaborator

There's a trailing backslash been left in at the end of this line.

@NelleV Collaborator
NelleV added a note

It's because the import takes two lines.

@dmcdougall Collaborator

In another PR (I can't remember which one) you just added an extra from matplotlib.blah import statement at the beginning of the wrapped line. There was also a PR that used ( and ) to encase the imported items so they would wrap well. Either of those approaches trumps this one, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@NelleV NelleV commented on the diff
lib/matplotlib/text.py
((7 lines not shown))
# these are not available for the object inspector until after the
# class is build so we define an initial set here for the init
# function and they will be overridden after object defn
-docstring.interpd.update(Text = """
@NelleV Collaborator
NelleV added a note

I should probably work on making the documentation prettier, a bit more...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dmcdougall dmcdougall commented on the diff
lib/matplotlib/text.py
@@ -839,7 +853,8 @@ def set_family(self, fontname):
the specific font names will be looked up in the
:file:`matplotlibrc` file.
- ACCEPTS: [ FONTNAME | 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
+ ACCEPTS: [FONTNAME | 'serif' | 'sans-serif' | 'cursive' | 'fantasy' |
+ 'monospace' ]
@dmcdougall Collaborator

Here this list was broken after the | binary operator. On line 70 it was broken before.

@NelleV Collaborator
NelleV added a note

I think it is best to break after the |. I'll change l. 70 to be consistent.

@dmcdougall Collaborator

Sure. To my eye I'm not fussed. I'm sure there will be fodder from others though. Thanks for being so patient and cooperative with us.

@NelleV Collaborator
NelleV added a note

Well, thanks for doing such boring reviews :)
Pep8 patches are always long tedious.

@dmcdougall Collaborator

They are, and you've had to make decisions about where to break things and what to modify. When opinions start getting passed around involving those decisions it can lead to frustrations and sometimes upset. It's good to see you're taking none of it personally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dmcdougall dmcdougall commented on the diff
lib/matplotlib/text.py
@@ -880,7 +895,8 @@ def set_size(self, fontsize):
Set the font size. May be either a size string, relative to
the default font size, or an absolute font size in points.
- ACCEPTS: [ size in points | 'xx-small' | 'x-small' | 'small' | 'medium' | 'large' | 'x-large' | 'xx-large' ]
+ ACCEPTS: [size in points | 'xx-small' | 'x-small' | 'small' |
+ 'medium' | 'large' | 'x-large' | 'xx-large' ]
@dmcdougall Collaborator

And here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dmcdougall dmcdougall commented on the diff
lib/matplotlib/text.py
@@ -892,7 +908,10 @@ def set_weight(self, weight):
"""
Set the font weight.
- ACCEPTS: [ a numeric value in range 0-1000 | 'ultralight' | 'light' | 'normal' | 'regular' | 'book' | 'medium' | 'roman' | 'semibold' | 'demibold' | 'demi' | 'bold' | 'heavy' | 'extra bold' | 'black' ]
+ ACCEPTS: [a numeric value in range 0-1000 | 'ultralight' | 'light' |
+ 'normal' | 'regular' | 'book' | 'medium' | 'roman' |
+ 'semibold' | 'demibold' | 'demi' | 'bold' | 'heavy' |
+ 'extra bold' | 'black' ]
@dmcdougall Collaborator

Ditto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dmcdougall dmcdougall commented on the diff
lib/matplotlib/text.py
@@ -904,7 +923,10 @@ def set_stretch(self, stretch):
"""
Set the font stretch (horizontal condensation or expansion).
- ACCEPTS: [ a numeric value in range 0-1000 | 'ultra-condensed' | 'extra-condensed' | 'condensed' | 'semi-condensed' | 'normal' | 'semi-expanded' | 'expanded' | 'extra-expanded' | 'ultra-expanded' ]
+ ACCEPTS: [a numeric value in range 0-1000 | 'ultra-condensed' |
+ 'extra-condensed' | 'condensed' | 'semi-condensed' |
+ 'normal' | 'semi-expanded' | 'expanded' | 'extra-expanded' |
+ 'ultra-expanded' ]
@dmcdougall Collaborator

And again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dmcdougall dmcdougall commented on the diff
lib/matplotlib/text.py
@@ -1508,7 +1529,8 @@ def _get_xy_transform(self, renderer, s):
bbox0 = self.axes.bbox
# elif bbox_name == "bbox":
# if bbox is None:
- # raise RuntimeError("bbox is specified as a coordinate but never set")
+ # raise RuntimeError("bbox is specified as a coordinate but "
+ # "never set")
# bbox0 = self._get_bbox(renderer, bbox)
@dmcdougall Collaborator

I wonder why this comment was left in...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/text.py
@@ -1549,21 +1571,21 @@ def _get_ref_xy(self, renderer):
if isinstance(self.xycoords, tuple):
s1, s2 = self.xycoords
- if (is_string_like(s1) and s1.split()[0] == "offset") \
- or (is_string_like(s2) and s2.split()[0] == "offset"):
+ if (is_string_like(s1) and s1.split()[0] == "offset") or \
@efiring Owner
efiring added a note

Here and below, I would prefer using parentheses rather than the trailing backslash.

@NelleV Collaborator
NelleV added a note

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/text.py
@@ -111,20 +114,18 @@ def _get_textbox(text, renderer):
projected_xs = []
projected_ys = []
- theta = text.get_rotation()/180.*math.pi
- tr = mtransforms.Affine2D().rotate(-theta)
+ theta = text.get_rotation() / 180. * math.pi
+ tr = mtransforms.Affine2D().rotate(- theta)
@efiring Owner
efiring added a note

Adding the space after the unary minus looks a bit odd, doesn't it?

@NelleV Collaborator
NelleV added a note

Yeah... I don't know why I did that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dmcdougall
Collaborator

@NelleV Can you squash down to one commit?

@NelleV
Collaborator

@dmcdougall Done !

lib/matplotlib/text.py
((11 lines not shown))
def _update_position_xytext(self, renderer, xy_pixel):
- "Update the pixel positions of the annotation text and the arrow patch."
+ """Update the pixel positions of the annotation text and the arrow
+ patch."""
@WeatherGod Collaborator

Multi-line docstrings should have the triple-quotes on their own lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@WeatherGod WeatherGod commented on the diff
lib/matplotlib/text.py
@@ -1892,10 +1915,11 @@ def _update_position_xytext(self, renderer, xy_pixel):
if self._bbox_patch:
self.arrow_patch.set_patchA(self._bbox_patch)
else:
- patchA = d.pop("patchA", None)
@WeatherGod Collaborator

Any clue what this was for? Could there still be code elsewhere that might need a "patchA" key popped out?

@NelleV Collaborator
NelleV added a note

I don't see where it would be needed, reading the code.

@WeatherGod Collaborator
@NelleV Collaborator
NelleV added a note

So should I put it back ?

@efiring Owner
efiring added a note

No, I would say you can leave it out, and we should close #1386. Note that the d.pop("patchA", None) is inside a block that is executed only if "patchA" is not in d, so it will never pop anything from d; all it does is to set "patchA = None", and patchA is not used from that point onward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/text.py
((11 lines not shown))
dsu.sort()
_, y = dsu[0]
shrink = d.pop('shrink', 0.0)
- theta = math.atan2(y-y0, x-x0)
- r = math.sqrt((y-y0)**2. + (x-x0)**2.)
- dx = shrink*r*math.cos(theta)
- dy = shrink*r*math.sin(theta)
+ theta = math.atan2(y - y0, x - x0)
+ r = math.sqrt((y - y0) ** 2. + (x - x0) ** 2.)
@WeatherGod Collaborator

another place to use math.hypot()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/text.py
@@ -1966,15 +1991,13 @@ def update_bbox_position_size(self, renderer):
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
self._bbox_patch.set_bounds(0., 0.,
w_box, h_box)
- theta = self.get_rotation()/180.*math.pi
+ theta = self.get_rotation() / 180. * math.pi
@WeatherGod Collaborator

Would this be more readable if we use np.deg2rad()?

@efiring Owner
efiring added a note

or math.radians, since we know we are dealing with a scalar and don't need numpy's machinery.

@dmcdougall Collaborator

Either is fine in my opinion, though I think I'd prefer just to stick with numpy.

Let's leave the decision up to @NelleV regarding which one to use. Once that change has been made I think this is fine to merge.

@NelleV Collaborator
NelleV added a note

I've changed all of those in np.deg2rad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/text.py
((6 lines not shown))
- horizLayout[i] = thisx, thisy-(d + d_yoffset), \
+ horizLayout[i] = thisx, thisy - (d + d_yoffset), \
@efiring Owner
efiring added a note

Instead of a trailing backslash, the RHS of this statement needs parentheses; it's a tuple.
Actually, I don't know why the line is broken at all; offhand, it looks like it would fit on a single line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/text.py
((8 lines not shown))
raise ValueError("xycoords should not be an offset coordinate")
x, y = self.xy
x1, y1 = self._get_xy(renderer, x, y, s1)
x2, y2 = self._get_xy(renderer, x, y, s2)
return x1, y2
- elif is_string_like(self.xycoords) and self.xycoords.split()[0] == "offset":
+ elif is_string_like(self.xycoords) and \
@efiring Owner
efiring added a note

Please use parentheses here instead of trailing backslash.

@NelleV Collaborator
NelleV added a note

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/text.py
((6 lines not shown))
elif unit == "pixels":
tr = Affine2D()
elif unit == "fontsize":
fontsize = self.get_size()
dpi = self.figure.get_dpi()
- tr = Affine2D().scale(fontsize*dpi/72., fontsize*dpi/72.)
+ tr = Affine2D().scale(fontsize * dpi / 72.,
+ fontsize * dpi / 72.)
@efiring Owner
efiring added a note

Minor suggestion, no insistence: since we have 4 instances of dpi / 72., and since it might not be completely obvious why, one could use a new variable, "dots_per_point" or "dpp" or "dppoint". I would probably use "dpp = dpi / 72.0" to keep it short and similar to "dpi". (And I like to include the zero after the decimal.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@NelleV
Collaborator

I get a pyflakes warning on this file:

 text.py|26| W402 'TextPath' imported but unused 

Should I delete it ?

@dmcdougall
Collaborator

I get a pyflakes warning on this file:

text.py|26| W402 'TextPath' imported but unused

Should I delete it ?

I'll open a separate issue for that once this is merged.

If nobody has any further objections, I'll merge this tomorrow.

@NelleV
Collaborator

I've rebased matplotlib/master on this branch: I think it is ready to be merged.

@dmcdougall dmcdougall merged commit c1b62af into matplotlib:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 246 additions and 225 deletions.
  1. +246 −225 lib/matplotlib/text.py
View
471 lib/matplotlib/text.py
@@ -13,20 +13,18 @@
from matplotlib.cbook import is_string_like, maxdict
from matplotlib import docstring
from matplotlib.font_manager import FontProperties
-from matplotlib.patches import bbox_artist, YAArrow, FancyBboxPatch, \
- FancyArrowPatch, Rectangle
+from matplotlib.patches import bbox_artist, YAArrow, FancyBboxPatch
+from matplotlib.patches import FancyArrowPatch, Rectangle
import matplotlib.transforms as mtransforms
-from matplotlib.transforms import Affine2D, Bbox, Transform ,\
- BboxBase, BboxTransformTo
+from matplotlib.transforms import Affine2D, Bbox, Transform
+from matplotlib.transforms import BboxBase, BboxTransformTo
from matplotlib.lines import Line2D
from matplotlib.artist import allow_rasterization
-from matplotlib.path import Path
-import matplotlib.font_manager as font_manager
-from matplotlib.ft2font import FT2Font
-
from matplotlib.backend_bases import RendererBase
+from matplotlib.textpath import TextPath
+
def _process_text_args(override, fontdict=None, **kwargs):
"Return an override dict. See :func:`~pyplot.text' docstring for info"
@@ -37,6 +35,7 @@ def _process_text_args(override, fontdict=None, **kwargs):
override.update(kwargs)
return override
+
# Extracted from Text's method to serve as a function
def get_rotation(rotation):
"""
@@ -50,57 +49,62 @@ def get_rotation(rotation):
angle = 90.
else:
angle = float(rotation)
- return angle%360
+ return angle % 360
+
# these are not available for the object inspector until after the
# class is build so we define an initial set here for the init
# function and they will be overridden after object defn
-docstring.interpd.update(Text = """
@NelleV Collaborator
NelleV added a note

I should probably work on making the documentation prettier, a bit more...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- ========================== =========================================================================
+docstring.interpd.update(Text="""
+ ========================== ================================================
Property Value
- ========================== =========================================================================
+ ========================== ================================================
alpha float or None
animated [True | False]
backgroundcolor any matplotlib color
- bbox rectangle prop dict plus key 'pad' which is a pad in points
+ bbox rectangle prop dict plus key 'pad' which is a
+ pad in points
clip_box a matplotlib.transform.Bbox instance
clip_on [True | False]
color any matplotlib color
- family [ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
+ family ['serif' | 'sans-serif' | 'cursive' |
+ 'fantasy' | 'monospace']
figure a matplotlib.figure.Figure instance
- fontproperties a matplotlib.font_manager.FontProperties instance
- horizontalalignment or ha [ 'center' | 'right' | 'left' ]
+ fontproperties a matplotlib.font_manager.FontProperties
+ instance
+ horizontalalignment or ha ['center' | 'right' | 'left']
label any string
linespacing float
lod [True | False]
multialignment ['left' | 'right' | 'center' ]
- name or fontname string eg, ['Sans' | 'Courier' | 'Helvetica' ...]
+ name or fontname string eg,
+ ['Sans' | 'Courier' | 'Helvetica' ...]
position (x,y)
rotation [ angle in degrees 'vertical' | 'horizontal'
rotation_mode [ None | 'anchor']
- size or fontsize [ size in points | relative size eg 'smaller', 'x-large' ]
+ size or fontsize [size in points | relative size eg 'smaller',
+ 'x-large']
style or fontstyle [ 'normal' | 'italic' | 'oblique']
text string
transform a matplotlib.transform transformation instance
- variant [ 'normal' | 'small-caps' ]
- verticalalignment or va [ 'center' | 'top' | 'bottom' | 'baseline' ]
+ variant ['normal' | 'small-caps']
+ verticalalignment or va ['center' | 'top' | 'bottom' | 'baseline']
visible [True | False]
- weight or fontweight [ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']
+ weight or fontweight ['normal' | 'bold' | 'heavy' | 'light' |
+ 'ultrabold' | 'ultralight']
x float
y float
zorder any number
- ========================== =========================================================================
+ ========================== ===============================================
""")
-
-
-
# TODO : This function may move into the Text class as a method. As a
# matter of fact, The information from the _get_textbox function
# should be available during the Text._get_layout() call, which is
# called within the _get_textbox. So, it would better to move this
# function as a method with some refactoring of _get_layout method.
+
def _get_textbox(text, renderer):
"""
Calculate the bounding box of the text. Unlike
@@ -111,20 +115,18 @@ def _get_textbox(text, renderer):
projected_xs = []
projected_ys = []
- theta = text.get_rotation()/180.*math.pi
+ theta = np.deg2rad(text.get_rotation())
tr = mtransforms.Affine2D().rotate(-theta)
for t, wh, x, y in text._get_layout(renderer)[1]:
w, h = wh
-
xt1, yt1 = tr.transform_point((x, y))
- xt2, yt2 = xt1+w, yt1+h
+ xt2, yt2 = xt1 + w, yt1 + h
projected_xs.extend([xt1, xt2])
projected_ys.extend([yt1, yt2])
-
xt_box, yt_box = min(projected_xs), min(projected_ys)
w_box, h_box = max(projected_xs) - xt_box, max(projected_ys) - yt_box
@@ -135,7 +137,6 @@ def _get_textbox(text, renderer):
return x_box, y_box, w_box, h_box
-
class Text(Artist):
"""
Handle storing and drawing of text in window or data coordinates.
@@ -145,15 +146,15 @@ class Text(Artist):
cached = maxdict(50)
def __str__(self):
- return "Text(%g,%g,%s)"%(self._x,self._y,repr(self._text))
+ return "Text(%g,%g,%s)" % (self._x, self._y, repr(self._text))
def __init__(self,
x=0, y=0, text='',
- color=None, # defaults to rc params
+ color=None, # defaults to rc params
verticalalignment='baseline',
horizontalalignment='left',
multialignment=None,
- fontproperties=None, # defaults to FontProperties()
+ fontproperties=None, # defaults to FontProperties()
rotation=None,
linespacing=None,
rotation_mode=None,
@@ -171,9 +172,12 @@ def __init__(self,
Artist.__init__(self)
self._x, self._y = x, y
- if color is None: color = rcParams['text.color']
- if fontproperties is None: fontproperties=FontProperties()
- elif is_string_like(fontproperties): fontproperties=FontProperties(fontproperties)
+ if color is None:
+ color = rcParams['text.color']
+ if fontproperties is None:
+ fontproperties = FontProperties()
+ elif is_string_like(fontproperties):
+ fontproperties = FontProperties(fontproperties)
self.set_path_effects(path_effects)
self.set_text(text)
@@ -184,7 +188,7 @@ def __init__(self,
self._rotation = rotation
self._fontproperties = fontproperties
self._bbox = None
- self._bbox_patch = None # a FancyBboxPatch instance
+ self._bbox_patch = None # a FancyBboxPatch instance
self._renderer = None
if linespacing is None:
linespacing = 1.2 # Maybe use rcParam later.
@@ -193,7 +197,7 @@ def __init__(self,
self.update(kwargs)
#self.set_bbox(dict(pad=0))
- def contains(self,mouseevent):
+ def contains(self, mouseevent):
"""Test whether the mouse event occurred in the patch.
In the case of text, a hit is true anywhere in the
@@ -201,14 +205,15 @@ def contains(self,mouseevent):
Returns True or False.
"""
- if callable(self._contains): return self._contains(self,mouseevent)
+ if callable(self._contains):
+ return self._contains(self, mouseevent)
if not self.get_visible() or self._renderer is None:
- return False,{}
+ return False, {}
+
+ l, b, w, h = self.get_window_extent().bounds
+ r, t = l + w, b + h
- l,b,w,h = self.get_window_extent().bounds
- r, t = l+w, b+h
-
x, y = mouseevent.x, mouseevent.y
inside = (l <= x <= r and b <= y <= t)
cattr = {}
@@ -216,20 +221,22 @@ def contains(self,mouseevent):
# if the text has a surrounding patch, also check containment for it,
# and merge the results with the results for the text.
if self._bbox_patch:
- patch_inside, patch_cattr = self._bbox_patch.contains(mouseevent)
+ patch_inside, patch_cattr = self._bbox_patch.contains(mouseevent)
inside = inside or patch_inside
cattr["bbox_patch"] = patch_cattr
- return inside, cattr
+ return inside, cattr
def _get_xy_display(self):
'get the (possibly unit converted) transformed x, y in display coords'
x, y = self.get_position()
- return self.get_transform().transform_point((x,y))
+ return self.get_transform().transform_point((x, y))
def _get_multialignment(self):
- if self._multialignment is not None: return self._multialignment
- else: return self._horizontalalignment
+ if self._multialignment is not None:
+ return self._multialignment
+ else:
+ return self._horizontalalignment
def get_rotation(self):
'return the text angle as float in degrees'
@@ -252,7 +259,6 @@ def get_rotation_mode(self):
"get text rotation mode"
return self._rotation_mode
-
def update_from(self, other):
'Copy properties from other to self'
Artist.update_from(self, other)
@@ -272,12 +278,13 @@ def _get_layout(self, renderer):
of a rotated text when necessary.
"""
key = self.get_prop_tup()
- if key in self.cached: return self.cached[key]
+ if key in self.cached:
+ return self.cached[key]
horizLayout = []
- thisx, thisy = 0.0, 0.0
- xmin, ymin = 0.0, 0.0
+ thisx, thisy = 0.0, 0.0
+ xmin, ymin = 0.0, 0.0
width, height = 0.0, 0.0
lines = self.get_text().split('\n')
@@ -288,12 +295,14 @@ def _get_layout(self, renderer):
from matplotlib.backends.backend_mixed import MixedModeRenderer
if isinstance(renderer, MixedModeRenderer):
def get_text_width_height_descent(*kl, **kwargs):
- return RendererBase.get_text_width_height_descent(renderer._renderer,
- *kl, **kwargs)
+ return RendererBase.get_text_width_height_descent(
+ renderer._renderer,
+ *kl, **kwargs)
else:
def get_text_width_height_descent(*kl, **kwargs):
- return RendererBase.get_text_width_height_descent(renderer,
- *kl, **kwargs)
+ return RendererBase.get_text_width_height_descent(
+ renderer,
+ *kl, **kwargs)
else:
get_text_width_height_descent = renderer.get_text_width_height_descent
@@ -326,18 +335,16 @@ def get_text_width_height_descent(*kl, **kwargs):
# of a "l" (e.g., use of superscripts), which seems
# what TeX does.
- d_yoffset = max(0, (h-d)-(lp_h-lp_bl))
+ d_yoffset = max(0, (h - d) - (lp_h - lp_bl))
- horizLayout[i] = thisx, thisy-(d + d_yoffset), \
- w, h
+ horizLayout[i] = thisx, thisy - (d + d_yoffset), w, h
baseline = (h - d) - thisy
thisy -= offsety + d_yoffset
width = max(width, w)
-
ymin = horizLayout[-1][1]
ymax = horizLayout[0][1] + horizLayout[0][3]
- height = ymax-ymin
+ height = ymax - ymin
xmax = xmin + width
# get the rotation matrix
@@ -346,10 +353,10 @@ def get_text_width_height_descent(*kl, **kwargs):
offsetLayout = np.zeros((len(lines), 2))
offsetLayout[:] = horizLayout[:, 0:2]
# now offset the individual text lines within the box
- if len(lines)>1: # do the multiline aligment
+ if len(lines) > 1: # do the multiline aligment
malign = self._get_multialignment()
if malign == 'center':
- offsetLayout[:, 0] += width/2.0 - horizLayout[:, 2] / 2.0
+ offsetLayout[:, 0] += width / 2.0 - horizLayout[:, 2] / 2.0
elif malign == 'right':
offsetLayout[:, 0] += width - horizLayout[:, 2]
@@ -366,42 +373,55 @@ def get_text_width_height_descent(*kl, **kwargs):
# compute the bounds of the rotated box
xmin, xmax = txs.min(), txs.max()
ymin, ymax = tys.min(), tys.max()
- width = xmax - xmin
+ width = xmax - xmin
height = ymax - ymin
- # Now move the box to the target position offset the display
+ # Now move the box to the target position offset the display
# bbox by alignment
halign = self._horizontalalignment
valign = self._verticalalignment
rotation_mode = self.get_rotation_mode()
- if rotation_mode != "anchor":
+ if rotation_mode != "anchor":
# compute the text location in display coords and the offsets
# necessary to align the bbox with that location
- if halign=='center': offsetx = (xmin + width/2.0)
- elif halign=='right': offsetx = (xmin + width)
- else: offsetx = xmin
-
- if valign=='center': offsety = (ymin + height/2.0)
- elif valign=='top': offsety = (ymin + height)
- elif valign=='baseline': offsety = (ymin + height) - baseline
- else: offsety = ymin
+ if halign == 'center':
+ offsetx = (xmin + width / 2.0)
+ elif halign == 'right':
+ offsetx = (xmin + width)
+ else:
+ offsetx = xmin
+
+ if valign == 'center':
+ offsety = (ymin + height / 2.0)
+ elif valign == 'top':
+ offsety = (ymin + height)
+ elif valign == 'baseline':
+ offsety = (ymin + height) - baseline
+ else:
+ offsety = ymin
else:
xmin1, ymin1 = cornersHoriz[0]
xmax1, ymax1 = cornersHoriz[2]
- if halign=='center': offsetx = (xmin1 + xmax1)/2.0
- elif halign=='right': offsetx = xmax1
- else: offsetx = xmin1
-
- if valign=='center': offsety = (ymin1 + ymax1)/2.0
- elif valign=='top': offsety = ymax1
- elif valign=='baseline': offsety = ymax1 - baseline
- else: offsety = ymin1
+ if halign == 'center':
+ offsetx = (xmin1 + xmax1) / 2.0
+ elif halign == 'right':
+ offsetx = xmax1
+ else:
+ offsetx = xmin1
+
+ if valign == 'center':
+ offsety = (ymin1 + ymax1) / 2.0
+ elif valign == 'top':
+ offsety = ymax1
+ elif valign == 'baseline':
+ offsety = ymax1 - baseline
+ else:
+ offsety = ymin1
offsetx, offsety = M.transform_point((offsetx, offsety))
-
xmin -= offsetx
ymin -= offsety
@@ -423,7 +443,6 @@ def set_path_effects(self, path_effects):
def get_path_effects(self):
return self._path_effects
-
def set_bbox(self, rectprops):
"""
Draw a bounding box around self. rectprops are any settable
@@ -448,18 +467,18 @@ def set_bbox(self, rectprops):
boxstyle = props.pop("boxstyle")
bbox_transmuter = props.pop("bbox_transmuter", None)
- self._bbox_patch = FancyBboxPatch((0., 0.),
- 1., 1.,
- boxstyle=boxstyle,
- bbox_transmuter=bbox_transmuter,
- transform=mtransforms.IdentityTransform(),
- **props)
+ self._bbox_patch = FancyBboxPatch(
+ (0., 0.),
+ 1., 1.,
+ boxstyle=boxstyle,
+ bbox_transmuter=bbox_transmuter,
+ transform=mtransforms.IdentityTransform(),
+ **props)
self._bbox = None
else:
self._bbox_patch = None
self._bbox = rectprops
-
def get_bbox_patch(self):
"""
Return the bbox Patch object. Returns None if the the
@@ -467,7 +486,6 @@ def get_bbox_patch(self):
"""
return self._bbox_patch
-
def update_bbox_position_size(self, renderer):
"""
Update the location and the size of the bbox. This method
@@ -489,15 +507,14 @@ def update_bbox_position_size(self, renderer):
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
self._bbox_patch.set_bounds(0., 0.,
w_box, h_box)
- theta = self.get_rotation()/180.*math.pi
+ theta = np.deg2rad(self.get_rotation())
tr = mtransforms.Affine2D().rotate(theta)
- tr = tr.translate(posx+x_box, posy+y_box)
+ tr = tr.translate(posx + x_box, posy + y_box)
self._bbox_patch.set_transform(tr)
fontsize_in_pixel = renderer.points_to_pixels(self.get_size())
self._bbox_patch.set_mutation_scale(fontsize_in_pixel)
#self._bbox_patch.draw(renderer)
-
def _draw_bbox(self, renderer, posx, posy):
""" Update the location and the size of the bbox
@@ -507,15 +524,14 @@ def _draw_bbox(self, renderer, posx, posy):
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
self._bbox_patch.set_bounds(0., 0.,
w_box, h_box)
- theta = self.get_rotation()/180.*math.pi
+ theta = np.deg2rad(self.get_rotation())
tr = mtransforms.Affine2D().rotate(theta)
- tr = tr.translate(posx+x_box, posy+y_box)
+ tr = tr.translate(posx + x_box, posy + y_box)
self._bbox_patch.set_transform(tr)
fontsize_in_pixel = renderer.points_to_pixels(self.get_size())
self._bbox_patch.set_mutation_scale(fontsize_in_pixel)
self._bbox_patch.draw(renderer)
-
@allow_rasterization
def draw(self, renderer):
"""
@@ -523,15 +539,16 @@ def draw(self, renderer):
"""
if renderer is not None:
self._renderer = renderer
- if not self.get_visible(): return
- if self.get_text()=='': return
+ if not self.get_visible():
+ return
+ if self.get_text() == '':
+ return
renderer.open_group('text', self.get_gid())
bbox, info = self._get_layout(renderer)
trans = self.get_transform()
-
# don't use self.get_position here, which refers to text position
# in Text, and dash position in TextWithDash:
posx = float(self.convert_xunits(self._x))
@@ -554,8 +571,6 @@ def draw(self, renderer):
bbox_artist(self, renderer, self._bbox)
angle = self.get_rotation()
-
-
if rcParams['text.usetex']:
for line, wh, x, y in info:
if not np.isfinite(x) or not np.isfinite(y):
@@ -564,7 +579,7 @@ def draw(self, renderer):
x = x + posx
y = y + posy
if renderer.flipy():
- y = canvash-y
+ y = canvash - y
clean_line, ismath = self.is_math_text(line)
if self.get_path_effects():
@@ -582,7 +597,7 @@ def draw(self, renderer):
x = x + posx
y = y + posy
if renderer.flipy():
- y = canvash-y
+ y = canvash - y
clean_line, ismath = self.is_math_text(line)
if self.get_path_effects():
@@ -677,7 +692,6 @@ def get_horizontalalignment(self):
"""
return self._horizontalalignment
-
def get_position(self):
"Return the position of the text as a tuple (*x*, *y*)"
x = float(self.convert_xunits(self._x))
@@ -736,13 +750,14 @@ def get_window_extent(self, renderer=None, dpi=None):
was used must be specified as the *dpi* argument.
'''
#return _unit_box
- if not self.get_visible(): return Bbox.unit()
+ if not self.get_visible():
+ return Bbox.unit()
if dpi is not None:
dpi_orig = self.figure.dpi
self.figure.dpi = dpi
if self.get_text() == '':
tx, ty = self._get_xy_display()
- return Bbox.from_bounds(tx,ty,0,0)
+ return Bbox.from_bounds(tx, ty, 0, 0)
if renderer is not None:
self._renderer = renderer
@@ -773,8 +788,6 @@ def set_backgroundcolor(self, color):
else:
self._bbox.update(dict(facecolor=color))
-
-
def set_color(self, color):
"""
Set the foreground color of the text
@@ -800,14 +813,14 @@ def set_horizontalalignment(self, align):
"""
legal = ('center', 'right', 'left')
if align not in legal:
- raise ValueError('Horizontal alignment must be one of %s' % str(legal))
+ raise ValueError('Horizontal alignment must be one of %s' %
+ str(legal))
self._horizontalalignment = align
def set_ma(self, align):
'alias for set_verticalalignment'
self.set_multialignment(align)
-
def set_multialignment(self, align):
"""
Set the alignment for multiple lines layout. The layout of the
@@ -819,7 +832,8 @@ def set_multialignment(self, align):
"""
legal = ('center', 'right', 'left')
if align not in legal:
- raise ValueError('Horizontal alignment must be one of %s' % str(legal))
+ raise ValueError('Horizontal alignment must be one of %s' %
+ str(legal))
self._multialignment = align
def set_linespacing(self, spacing):
@@ -839,7 +853,8 @@ def set_family(self, fontname):
the specific font names will be looked up in the
:file:`matplotlibrc` file.
- ACCEPTS: [ FONTNAME | 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
+ ACCEPTS: [FONTNAME | 'serif' | 'sans-serif' | 'cursive' | 'fantasy' |
+ 'monospace' ]
@dmcdougall Collaborator

Here this list was broken after the | binary operator. On line 70 it was broken before.

@NelleV Collaborator
NelleV added a note

I think it is best to break after the |. I'll change l. 70 to be consistent.

@dmcdougall Collaborator

Sure. To my eye I'm not fussed. I'm sure there will be fodder from others though. Thanks for being so patient and cooperative with us.

@NelleV Collaborator
NelleV added a note

Well, thanks for doing such boring reviews :)
Pep8 patches are always long tedious.

@dmcdougall Collaborator

They are, and you've had to make decisions about where to break things and what to modify. When opinions start getting passed around involving those decisions it can lead to frustrations and sometimes upset. It's good to see you're taking none of it personally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
"""
self._fontproperties.set_family(fontname)
@@ -880,7 +895,8 @@ def set_size(self, fontsize):
Set the font size. May be either a size string, relative to
the default font size, or an absolute font size in points.
- ACCEPTS: [ size in points | 'xx-small' | 'x-small' | 'small' | 'medium' | 'large' | 'x-large' | 'xx-large' ]
+ ACCEPTS: [size in points | 'xx-small' | 'x-small' | 'small' |
+ 'medium' | 'large' | 'x-large' | 'xx-large' ]
@dmcdougall Collaborator

And here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
"""
self._fontproperties.set_size(fontsize)
@@ -892,7 +908,10 @@ def set_weight(self, weight):
"""
Set the font weight.
- ACCEPTS: [ a numeric value in range 0-1000 | 'ultralight' | 'light' | 'normal' | 'regular' | 'book' | 'medium' | 'roman' | 'semibold' | 'demibold' | 'demi' | 'bold' | 'heavy' | 'extra bold' | 'black' ]
+ ACCEPTS: [a numeric value in range 0-1000 | 'ultralight' | 'light' |
+ 'normal' | 'regular' | 'book' | 'medium' | 'roman' |
+ 'semibold' | 'demibold' | 'demi' | 'bold' | 'heavy' |
+ 'extra bold' | 'black' ]
@dmcdougall Collaborator

Ditto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
"""
self._fontproperties.set_weight(weight)
@@ -904,7 +923,10 @@ def set_stretch(self, stretch):
"""
Set the font stretch (horizontal condensation or expansion).
- ACCEPTS: [ a numeric value in range 0-1000 | 'ultra-condensed' | 'extra-condensed' | 'condensed' | 'semi-condensed' | 'normal' | 'semi-expanded' | 'expanded' | 'extra-expanded' | 'ultra-expanded' ]
+ ACCEPTS: [a numeric value in range 0-1000 | 'ultra-condensed' |
+ 'extra-condensed' | 'condensed' | 'semi-condensed' |
+ 'normal' | 'semi-expanded' | 'expanded' | 'extra-expanded' |
+ 'ultra-expanded' ]
@dmcdougall Collaborator

And again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
"""
self._fontproperties.set_stretch(stretch)
@@ -929,7 +951,6 @@ def set_x(self, x):
"""
self._x = x
-
def set_y(self, y):
"""
Set the *y* position of the text
@@ -938,7 +959,6 @@ def set_y(self, y):
"""
self._y = y
-
def set_rotation(self, s):
"""
Set the rotation of the text
@@ -947,8 +967,6 @@ def set_rotation(self, s):
"""
self._rotation = s
-
-
def set_va(self, align):
'alias for set_verticalalignment'
self.set_verticalalignment(align)
@@ -961,7 +979,8 @@ def set_verticalalignment(self, align):
"""
legal = ('top', 'bottom', 'center', 'baseline')
if align not in legal:
- raise ValueError('Vertical alignment must be one of %s' % str(legal))
+ raise ValueError('Vertical alignment must be one of %s' %
+ str(legal))
self._verticalalignment = align
@@ -1011,7 +1030,7 @@ def set_font_properties(self, fp):
'alias for set_fontproperties'
self.set_fontproperties(fp)
-docstring.interpd.update(Text = artist.kwdoc(Text))
+docstring.interpd.update(Text=artist.kwdoc(Text))
docstring.dedent_interpd(Text.__init__)
@@ -1072,14 +1091,15 @@ class TextWithDash(Text):
__name__ = 'textwithdash'
def __str__(self):
- return "TextWithDash(%g,%g,%s)"%(self._x,self._y,repr(self._text))
+ return "TextWithDash(%g,%g,%s)" % (self._x, self._y, repr(self._text))
+
def __init__(self,
x=0, y=0, text='',
color=None, # defaults to rc params
verticalalignment='center',
horizontalalignment='center',
multialignment=None,
- fontproperties=None, # defaults to FontProperties()
+ fontproperties=None, # defaults to FontProperties()
rotation=None,
linespacing=None,
dashlength=0.0,
@@ -1131,7 +1151,9 @@ def get_prop_tup(self):
need to know if the text has changed.
"""
props = [p for p in Text.get_prop_tup(self)]
- props.extend([self._x, self._y, self._dashlength, self._dashdirection, self._dashrotation, self._dashpad, self._dashpush])
+ props.extend([self._x, self._y, self._dashlength,
+ self._dashdirection, self._dashrotation, self._dashpad,
+ self._dashpush])
return tuple(props)
def draw(self, renderer):
@@ -1163,7 +1185,7 @@ def update_coords(self, renderer):
dashpush = self.get_dashpush()
angle = get_rotation(dashrotation)
- theta = np.pi*(angle/180.0+dashdirection-1)
+ theta = np.pi * (angle / 180.0 + dashdirection - 1)
cos_theta, sin_theta = np.cos(theta), np.sin(theta)
transform = self.get_transform()
@@ -1172,8 +1194,8 @@ def update_coords(self, renderer):
# The 'c' prefix is for canvas coordinates
cxy = transform.transform_point((dashx, dashy))
cd = np.array([cos_theta, sin_theta])
- c1 = cxy+dashpush*cd
- c2 = cxy+(dashpush+dashlength)*cd
+ c1 = cxy + dashpush * cd
+ c2 = cxy + (dashpush + dashlength) * cd
inverse = transform.inverted()
(x1, y1) = inverse.transform_point(tuple(c1))
@@ -1204,15 +1226,15 @@ def update_coords(self, renderer):
dx = 0.0
dy = h
else:
- tan_theta = sin_theta/cos_theta
+ tan_theta = sin_theta / cos_theta
dx = w
- dy = w*tan_theta
+ dy = w * tan_theta
if dy > h or dy < -h:
dy = h
- dx = h/tan_theta
- cwd = np.array([dx, dy])/2
- cwd *= 1+dashpad/np.sqrt(np.dot(cwd,cwd))
- cw = c2+(dashdirection*2-1)*cwd
+ dx = h / tan_theta
+ cwd = np.array([dx, dy]) / 2
+ cwd *= 1 + dashpad / np.sqrt(np.dot(cwd, cwd))
+ cw = c2 + (dashdirection * 2 - 1) * cwd
newx, newy = inverse.transform_point(tuple(cw))
self._x, self._y = newx, newy
@@ -1283,7 +1305,7 @@ def get_dashrotation(self):
"""
Get the rotation of the dash in degrees.
"""
- if self._dashrotation == None:
+ if self._dashrotation is None:
return self.get_rotation()
else:
return self._dashrotation
@@ -1328,7 +1350,6 @@ def set_dashpush(self, dp):
"""
self._dashpush = dp
-
def set_position(self, xy):
"""
Set the (*x*, *y*) position of the :class:`TextWithDash`.
@@ -1383,7 +1404,7 @@ def set_figure(self, fig):
class OffsetFrom(object):
def __init__(self, artist, ref_coord, unit="points"):
self._artist = artist
- self._ref_coord= ref_coord
+ self._ref_coord = ref_coord
self.set_unit(unit)
def set_unit(self, unit):
@@ -1394,7 +1415,7 @@ def get_unit(self):
return self._unit
def _get_scale(self, renderer):
- unit = self.get_unit()
+ unit = self.get_unit()
if unit == "pixels":
return 1.
else:
@@ -1405,11 +1426,11 @@ def __call__(self, renderer):
bbox = self._artist.get_window_extent(renderer)
l, b, w, h = bbox.bounds
xf, yf = self._ref_coord
- x, y = l+w*xf, b+h*yf
+ x, y = l + w * xf, b + h * yf
elif isinstance(self._artist, BboxBase):
l, b, w, h = self._artist.bounds
xf, yf = self._ref_coord
- x, y = l+w*xf, b+h*yf
+ x, y = l + w * xf, b + h * yf
elif isinstance(self._artist, Transform):
x, y = self._artist.transform_point(self._ref_coord)
else:
@@ -1420,6 +1441,7 @@ def __call__(self, renderer):
return tr
+
class _AnnotationBase(object):
def __init__(self,
xy, xytext=None,
@@ -1430,7 +1452,7 @@ def __init__(self,
if textcoords is None:
textcoords = xycoords
# we'll draw ourself after the artist we annotate by default
- x,y = self.xytext = xytext
+ x, y = self.xytext = xytext
self.xy = xy
self.xycoords = xycoords
@@ -1450,9 +1472,8 @@ def _get_xy(self, renderer, x, y, s):
if s2 == 'data':
y = float(self.convert_yunits(y))
-
if s in ['axes points', 'axes pixel', 'figure points', 'figure pixel']:
- return self._get_xy_legacy(renderer, x, y, s)
+ return self._get_xy_legacy(renderer, x, y, s)
tr = self._get_xy_transform(renderer, s)
x1, y1 = tr.transform_point((x, y))
@@ -1486,9 +1507,9 @@ def _get_xy_transform(self, renderer, s):
elif not is_string_like(s):
raise RuntimeError("unknown coordinate type : %s" % (s,))
- if s=='data':
+ if s == 'data':
return self.axes.transData
- elif s=='polar':
+ elif s == 'polar':
from matplotlib.projections import PolarAxes
tr = PolarAxes.PolarTransform()
trans = tr + self.axes.transData
@@ -1508,7 +1529,8 @@ def _get_xy_transform(self, renderer, s):
bbox0 = self.axes.bbox
# elif bbox_name == "bbox":
# if bbox is None:
- # raise RuntimeError("bbox is specified as a coordinate but never set")
+ # raise RuntimeError("bbox is specified as a coordinate but "
+ # "never set")
# bbox0 = self._get_bbox(renderer, bbox)
@dmcdougall Collaborator

I wonder why this comment was left in...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
if bbox0 is not None:
@@ -1521,14 +1543,16 @@ def _get_xy_transform(self, renderer, s):
ref_x, ref_y = xy0
from matplotlib.transforms import Affine2D
if unit == "points":
- dpi = self.figure.get_dpi()
- tr = Affine2D().scale(dpi/72., dpi/72.)
+ # dots per points
+ dpp = self.figure.get_dpi() / 12.
+ tr = Affine2D().scale(dpp, dpp)
elif unit == "pixels":
tr = Affine2D()
elif unit == "fontsize":
fontsize = self.get_size()
- dpi = self.figure.get_dpi()
- tr = Affine2D().scale(fontsize*dpi/72., fontsize*dpi/72.)
+ dpp = fontsize * self.figure.get_dpi() / 72.
+ tr = Affine2D().scale(dpp,
+ dpp)
elif unit == "fraction":
w, h = bbox0.bounds[2:]
tr = Affine2D().scale(w, h)
@@ -1540,7 +1564,6 @@ def _get_xy_transform(self, renderer, s):
else:
raise ValueError("%s is not a recognized coodinate" % s)
-
def _get_ref_xy(self, renderer):
"""
return x, y (in display coordinate) that is to be used for a reference
@@ -1549,21 +1572,21 @@ def _get_ref_xy(self, renderer):
if isinstance(self.xycoords, tuple):
s1, s2 = self.xycoords
- if (is_string_like(s1) and s1.split()[0] == "offset") \
- or (is_string_like(s2) and s2.split()[0] == "offset"):
+ if ((is_string_like(s1) and s1.split()[0] == "offset") or
+ (is_string_like(s2) and s2.split()[0] == "offset")):
raise ValueError("xycoords should not be an offset coordinate")
x, y = self.xy
x1, y1 = self._get_xy(renderer, x, y, s1)
x2, y2 = self._get_xy(renderer, x, y, s2)
return x1, y2
- elif is_string_like(self.xycoords) and self.xycoords.split()[0] == "offset":
+ elif (is_string_like(self.xycoords) and
+ self.xycoords.split()[0] == "offset"):
raise ValueError("xycoords should not be an offset coordinate")
else:
x, y = self.xy
return self._get_xy(renderer, x, y, self.xycoords)
#raise RuntimeError("must be defined by the derived class")
-
# def _get_bbox(self, renderer):
# if hasattr(bbox, "bounds"):
# return bbox
@@ -1571,13 +1594,13 @@ def _get_ref_xy(self, renderer):
# bbox = bbox.get_window_extent()
# return bbox
# else:
- # raise ValueError("A bbox instance is expected but got %s" % str(bbox))
-
-
+ # raise ValueError("A bbox instance is expected but got %s" %
+ # str(bbox))
def _get_xy_legacy(self, renderer, x, y, s):
"""
- only used when s in ['axes points', 'axes pixel', 'figure points', 'figure pixel'].
+ only used when s in ['axes points', 'axes pixel', 'figure points',
+ 'figure pixel'].
"""
s_ = s.split()
bbox0, xy0 = None, None
@@ -1589,30 +1612,31 @@ def _get_xy_legacy(self, renderer, x, y, s):
bbox0 = self.axes.bbox
if unit == "points":
- sc = self.figure.get_dpi()/72.
+ sc = self.figure.get_dpi() / 72.
elif unit == "pixels":
sc = 1
- l,b,r,t = bbox0.extents
- if x<0:
- x = r + x*sc
+ l, b, r, t = bbox0.extents
+ if x < 0:
+ x = r + x * sc
else:
- x = l + x*sc
- if y<0:
- y = t + y*sc
+ x = l + x * sc
+ if y < 0:
+ y = t + y * sc
else:
- y = b + y*sc
+ y = b + y * sc
return x, y
-
def set_annotation_clip(self, b):
"""
set *annotation_clip* attribute.
- * True : the annotation will only be drawn when self.xy is inside the axes.
- * False : the annotation will always be drawn regardless of its position.
- * None : the self.xy will be checked only if *xycoords* is "data"
+ * True: the annotation will only be drawn when self.xy is inside
+ the axes.
+ * False: the annotation will always be drawn regardless of its
+ position.
+ * None: the self.xy will be checked only if *xycoords* is "data"
"""
self._annotation_clip = b
@@ -1643,7 +1667,6 @@ def _check_xy(self, renderer, xy_pixel):
return True
-
def draggable(self, state=None, use_blit=False):
"""
Set the draggable state -- if state is
@@ -1684,7 +1707,10 @@ class Annotation(Text, _AnnotationBase):
:class:`~matplotlib.patches.Rectangle`, etc., easier.
"""
def __str__(self):
- return "Annotation(%g,%g,%s)"%(self.xy[0],self.xy[1],repr(self._text))
+ return "Annotation(%g,%g,%s)" % (self.xy[0],
+ self.xy[1],
+ repr(self._text))
+
@docstring.dedent_interpd
def __init__(self, s, xy,
xytext=None,
@@ -1708,9 +1734,9 @@ def __init__(self, s, xy,
drawn. Valid keys for YAArow are
- ========= =============================================================
+ ========= ===========================================================
Key Description
- ========= =============================================================
+ ========= ===========================================================
width the width of the arrow in points
frac the fraction of the arrow length occupied by the head
headwidth the width of the base of the arrow head in points
@@ -1718,10 +1744,10 @@ def __init__(self, s, xy,
and base a bit away from the text and point being
annotated. If *d* is the distance between the text and
annotated point, shrink will shorten the arrow so the tip
- and base are shink percent of the distance *d* away from the
- endpoints. ie, ``shrink=0.05 is 5%%``
+ and base are shink percent of the distance *d* away from
+ the endpoints. ie, ``shrink=0.05 is 5%%``
? any key for :class:`matplotlib.patches.polygon`
- ========= =============================================================
+ ========= ===========================================================
Valid keys for FancyArrowPatch are
@@ -1799,7 +1825,7 @@ def __init__(self, s, xy,
xycoords=xycoords, textcoords=textcoords,
annotation_clip=annotation_clip)
- x,y = self.xytext
+ x, y = self.xytext
Text.__init__(self, x, y, s, **kwargs)
self.arrowprops = arrowprops
@@ -1809,13 +1835,13 @@ def __init__(self, s, xy,
if arrowprops and "arrowstyle" in arrowprops:
arrowprops = self.arrowprops.copy()
self._arrow_relpos = arrowprops.pop("relpos", (0.5, 0.5))
- self.arrow_patch = FancyArrowPatch((0, 0), (1,1),
+ self.arrow_patch = FancyArrowPatch((0, 0), (1, 1),
**arrowprops)
else:
self.arrow_patch = None
- def contains(self,event):
- contains, tinfo = Text.contains(self,event)
+ def contains(self, event):
+ contains, tinfo = Text.contains(self, event)
if self.arrow is not None:
in_arrow, _ = self.arrow.contains(event)
contains = contains or in_arrow
@@ -1831,21 +1857,22 @@ def set_figure(self, fig):
self.arrow_patch.set_figure(fig)
Artist.set_figure(self, fig)
-
def update_positions(self, renderer):
- "Update the pixel positions of the annotated point and the text."
+ """"Update the pixel positions of the annotated point and the
+ text.
+ """
xy_pixel = self._get_position_xy(renderer)
self._update_position_xytext(renderer, xy_pixel)
-
def _update_position_xytext(self, renderer, xy_pixel):
- "Update the pixel positions of the annotation text and the arrow patch."
+ """Update the pixel positions of the annotation text and the arrow
+ patch.
+ """
x, y = self.xytext
self._x, self._y = self._get_xy(renderer, x, y,
self.textcoords)
-
x, y = xy_pixel
ox0, oy0 = self._x, self._y
@@ -1853,11 +1880,11 @@ def _update_position_xytext(self, renderer, xy_pixel):
if self.arrowprops:
x0, y0 = x, y
- l,b,w,h = self.get_window_extent(renderer).bounds
- r = l+w
- t = b+h
- xc = 0.5*(l+r)
- yc = 0.5*(b+t)
+ l, b, w, h = self.get_window_extent(renderer).bounds
+ r = l + w
+ t = b + h
+ xc = 0.5 * (l + r)
+ yc = 0.5 * (b + t)
d = self.arrowprops.copy()
@@ -1881,7 +1908,7 @@ def _update_position_xytext(self, renderer, xy_pixel):
# (in points). If patch A is not set, self.bbox_patch
# is used.
- self.arrow_patch.set_positions((ox0, oy0), (ox1,oy1))
+ self.arrow_patch.set_positions((ox0, oy0), (ox1, oy1))
mutation_scale = d.pop("mutation_scale", self.get_size())
mutation_scale = renderer.points_to_pixels(mutation_scale)
self.arrow_patch.set_mutation_scale(mutation_scale)
@@ -1892,10 +1919,11 @@ def _update_position_xytext(self, renderer, xy_pixel):
if self._bbox_patch:
self.arrow_patch.set_patchA(self._bbox_patch)
else:
- patchA = d.pop("patchA", None)
@WeatherGod Collaborator

Any clue what this was for? Could there still be code elsewhere that might need a "patchA" key popped out?

@NelleV Collaborator
NelleV added a note

I don't see where it would be needed, reading the code.

@WeatherGod Collaborator
@NelleV Collaborator
NelleV added a note

So should I put it back ?

@efiring Owner
efiring added a note

No, I would say you can leave it out, and we should close #1386. Note that the d.pop("patchA", None) is inside a block that is executed only if "patchA" is not in d, so it will never pop anything from d; all it does is to set "patchA = None", and patchA is not used from that point onward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
props = self._bbox
- if props is None: props = {}
- props = props.copy() # don't want to alter the pad externally
+ if props is None:
+ props = {}
+ # don't want to alter the pad externally
+ props = props.copy()
pad = props.pop('pad', 4)
pad = renderer.points_to_pixels(pad)
if self.get_text() == "":
@@ -1903,17 +1931,17 @@ def _update_position_xytext(self, renderer, xy_pixel):
return
bbox = self.get_window_extent(renderer)
- l,b,w,h = bbox.bounds
- l-=pad/2.
- b-=pad/2.
- w+=pad
- h+=pad
- r = Rectangle(xy=(l,b),
+ l, b, w, h = bbox.bounds
+ l -= pad / 2.
+ b -= pad / 2.
+ w += pad
+ h += pad
+ r = Rectangle(xy=(l, b),
width=w,
height=h,
)
r.set_transform(mtransforms.IdentityTransform())
- r.set_clip_on( False )
+ r.set_clip_on(False)
r.update(props)
self.arrow_patch.set_patchA(r)
@@ -1922,32 +1950,33 @@ def _update_position_xytext(self, renderer, xy_pixel):
# pick the x,y corner of the text bbox closest to point
# annotated
- dsu = [(abs(val-x0), val) for val in (l, r, xc)]
+ dsu = [(abs(val - x0), val) for val in (l, r, xc)]
dsu.sort()
_, x = dsu[0]
- dsu = [(abs(val-y0), val) for val in (b, t, yc)]
+ dsu = [(abs(val - y0), val) for val in (b, t, yc)]
dsu.sort()
_, y = dsu[0]
shrink = d.pop('shrink', 0.0)
- theta = math.atan2(y-y0, x-x0)
- r = math.sqrt((y-y0)**2. + (x-x0)**2.)
- dx = shrink*r*math.cos(theta)
- dy = shrink*r*math.sin(theta)
+ theta = math.atan2(y - y0, x - x0)
+ r = np.hypot((y - y0), (x - x0))
+ dx = shrink * r * math.cos(theta)
+ dy = shrink * r * math.sin(theta)
width = d.pop('width', 4)
headwidth = d.pop('headwidth', 12)
frac = d.pop('frac', 0.1)
- self.arrow = YAArrow(self.figure, (x0+dx,y0+dy), (x-dx, y-dy),
- width=width, headwidth=headwidth, frac=frac,
+ self.arrow = YAArrow(self.figure,
+ (x0 + dx, y0 + dy), (x - dx, y - dy),
+ width=width, headwidth=headwidth,
+ frac=frac,
**d)
self.arrow.set_clip_box(self.get_clip_box())
-
def update_bbox_position_size(self, renderer):
"""
Update the location and the size of the bbox. This method
@@ -1966,15 +1995,13 @@ def update_bbox_position_size(self, renderer):
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
self._bbox_patch.set_bounds(0., 0.,
w_box, h_box)
- theta = self.get_rotation()/180.*math.pi
+ theta = np.deg2rad(self.get_rotation())
tr = mtransforms.Affine2D().rotate(theta)
- tr = tr.translate(posx+x_box, posy+y_box)
+ tr = tr.translate(posx + x_box, posy + y_box)
self._bbox_patch.set_transform(tr)
fontsize_in_pixel = renderer.points_to_pixels(self.get_size())
self._bbox_patch.set_mutation_scale(fontsize_in_pixel)
-
-
@allow_rasterization
def draw(self, renderer):
"""
@@ -1983,7 +2010,8 @@ def draw(self, renderer):
if renderer is not None:
self._renderer = renderer
- if not self.get_visible(): return
+ if not self.get_visible():
+ return
xy_pixel = self._get_position_xy(renderer)
if not self._check_xy(renderer, xy_pixel):
@@ -1992,7 +2020,6 @@ def draw(self, renderer):
self._update_position_xytext(renderer, xy_pixel)
self.update_bbox_position_size(renderer)
-
if self.arrow is not None:
if self.arrow.figure is None and self.figure is not None:
self.arrow.figure = self.figure
@@ -2006,10 +2033,4 @@ def draw(self, renderer):
Text.draw(self, renderer)
-
-
-
docstring.interpd.update(Annotation=Annotation.__init__.__doc__)
-
-
-from matplotlib.textpath import TextPath
Something went wrong with that request. Please try again.