8000 Annotation: always use FancyBboxPatch instead of bbox_artist by efiring · Pull Request #4178 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Annotation: always use FancyBboxPatch instead of bbox_artist #4178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 22, 2015
Next Next commit
Annotation: always use FancyBboxPatch instead of bbox_artist
  • Loading branch information
efiring committed Jul 19, 2015
commit 9d933bc794f228cbbed675d11e10fd63da406911
45 changes: 13 additions & 32 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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
from matplotlib.patches import YAArrow, FancyBboxPatch
from matplotlib.patches import FancyArrowPatch, Rectangle
import matplotlib.transforms as mtransforms
from matplotlib.transforms import Affine2D, Bbox, Transform
Expand Down Expand Up @@ -469,25 +469,19 @@ def _get_layout(self, renderer):
def set_bbox(self, rectprops):
"""
Draw a bounding box around self. rectprops are any settable
properties for a rectangle, e.g., facecolor='red', alpha=0.5.
properties for a FancyBboxPatch, e.g., facecolor='red', alpha=0.5.

t.set_bbox(dict(facecolor='red', alpha=0.5))

If rectprops has "boxstyle" key. A FancyBboxPatch
is initialized with rectprops and will be drawn. The mutation
scale of the FancyBboxPath is set to the fontsize.
The default boxstyle is 'square'. The mutation
scale of the FancyBboxPatch is set to the fontsize.

ACCEPTS: rectangle prop dict
ACCEPTS: FancyBboxPatch prop dict
"""

# The self._bbox_patch object is created only if rectprops has
# boxstyle key. Otherwise, self._bbox will be set to the
# rectprops and the bbox will be drawn using bbox_artist
# function. This is to keep the backward compatibility.

if rectprops is not None and "boxstyle" in rectprops:
if rectprops is not None:
props = rectprops.copy()
boxstyle = props.pop("boxstyle")
boxstyle = props.pop("boxstyle", "square")
bbox_transmuter = props.pop("bbox_transmuter", None)

self._bbox_patch = FancyBboxPatch(
Expand All @@ -497,10 +491,8 @@ def set_bbox(self, rectprops):
bbox_transmuter=bbox_transmuter,
transform=mtransforms.IdentityTransform(),
**props)
self._bbox = None
else:
self._bbox_patch = None
self._bbox = rectprops

self._update_clip_properties()

Expand Down Expand Up @@ -542,7 +534,7 @@ def update_bbox_position_size(self, renderer):
def _draw_bbox(self, renderer, posx, posy):

""" Update the location and the size of the bbox
(FancyBoxPatch), and draw
(FancyBboxPatch), and draw
"""

x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
Expand All @@ -560,8 +552,6 @@ def _update_clip_properties(self):
clip_path=self._clippath,
clip_on=self._clipon)

if self._bbox:
bbox = self._bbox.update(clipprops)
if self._bbox_patch:
bbox = self._bbox_patch.update(clipprops)

Expand Down Expand Up @@ -756,8 +746,6 @@ def draw(self, renderer):
gc.set_url(textobj._url)
textobj._set_gc_clip(gc)

if textobj._bbox:
bbox_artist(textobj, renderer, textobj._bbox)
angle = textobj.get_rotation()

for line, wh, x, y in info:
Expand Down Expand Up @@ -959,10 +947,10 @@ def set_backgroundcolor(self, color):

ACCEPTS: any matplotlib color
"""
if self._bbox is None:
self._bbox = dict(facecolor=color, edgecolor=color)
if self._bbox_patch is None:
self.set_bbox = dict(facecolor=color, edgecolor=color)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a function call?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

else:
self._bbox.update(dict(facecolor=color))
self._bbox_patch.update(dict(facecolor=color))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be de-dented

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? As it is, the update of the existing _bbox_patch is needed only if one hasn't just created a new one with the desired color.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tacaswell, your comment on this line, and my response, were lost when I rebased. You thought the line should be dedented, but I don't understand why. The update is needed only if a new _bbox_patch has not just been created.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember exactly what I was thinking when I left the last message. I think what I was suggesting was to get rid of the else block and just always update the _bbox_patch (even if we just created it with the correct color). It is a no-op and I think makes the code path a little easier to follow (which is a style call).

Avoiding else statements and having if blocks which just ensure assumptions are true is is a newish (and possibly not good) style-tick I have developed.

I think my comment on this can be ignored.


self._update_clip_properties()
self.stale = True
Expand Down Expand Up @@ -2147,13 +2135,7 @@ def _update_position_xytext(self, renderer, xy_pixel):
if self._bbox_patch:
self.arrow_patch.set_patchA(self._bbox_patch)
else:
props = self._bbox
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)
pad = renderer.points_to_pixels(4)
if self.get_text().strip() == "":
self.arrow_patch.set_patchA(None)
return
Expand All @@ -2170,12 +2152,11 @@ def _update_position_xytext(self, renderer, xy_pixel):
)
r.set_transform(mtransforms.IdentityTransform())
r.set_clip_on(False)
r.update(props)

self.arrow_patch.set_patchA(r)

else:

# using YAArrow
# pick the x,y corner of the text bbox closest to point
# annotated
dsu = [(abs(val - x0), val) for val in (l, r, xc)]
Expand Down
0