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

Skip to content

Commit 0089263

Browse files
committed
Remove Patch visibility tracking by Legend & OffsetBox.
Instead of having a separate _drawFrame attribute on Legend/OffsetBox, just set the visibility attribute on the underlying Patch.
1 parent ac01f12 commit 0089263

File tree

3 files changed

+34
-49
lines changed

3 files changed

+34
-49
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,9 @@ anymore.
186186
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187187
This ensures that they go above gridlines by default. The old ``zorder`` can
188188
be kept by passing it as a keyword argument to `.Axes.hist`.
189+
190+
`.Legend` and `.OffsetBox` visibility
191+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192+
`.Legend` and `.OffsetBox` subclasses (`.PaddedBox`, `.AnchoredOffsetbox`, and
193+
`.AnnotationBbox`) no longer directly keep track of the visibility of their
194+
underlying `.Patch` artist, but instead pass that flag down to the `.Patch`.

lib/matplotlib/legend.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,11 @@ def __init__(self, parent, handles, labels,
499499
else "square,pad=0"),
500500
mutation_scale=self._fontsize,
501501
snap=True,
502+
visible=(frameon if frameon is not None
503+
else mpl.rcParams["legend.frameon"])
502504
)
503505
self._set_artist_props(self.legendPatch)
504506

505-
self._drawFrame = (frameon if frameon is not None
506-
else mpl.rcParams["legend.frameon"])
507-
508507
# init with null renderer
509508
self._init_legend_box(handles, labels, markerfirst)
510509

@@ -580,17 +579,13 @@ def draw(self, renderer):
580579
# update the location and size of the legend. This needs to
581580
# be done in any case to clip the figure right.
582581
bbox = self._legend_box.get_window_extent(renderer)
583-
self.legendPatch.set_bounds(bbox.x0, bbox.y0,
584-
bbox.width, bbox.height)
582+
self.legendPatch.set_bounds(bbox.x0, bbox.y0, bbox.width, bbox.height)
585583
self.legendPatch.set_mutation_scale(fontsize)
586584

587-
if self._drawFrame:
588-
if self.shadow:
589-
shadow = Shadow(self.legendPatch, 2, -2)
590-
shadow.draw(renderer)
591-
592-
self.legendPatch.draw(renderer)
585+
if self.shadow:
586+
Shadow(self.legendPatch, 2, -2).draw(renderer)
593587

588+
self.legendPatch.draw(renderer)
594589
self._legend_box.draw(renderer)
595590

596591
renderer.close_group('legend')
@@ -895,7 +890,7 @@ def get_tightbbox(self, renderer):
895890

896891
def get_frame_on(self):
897892
"""Get whether the legend box patch is drawn."""
898-
return self._drawFrame
893+
return self.legendPatch.get_visible()
899894

900895
def set_frame_on(self, b):
901896
"""
@@ -905,7 +900,7 @@ def set_frame_on(self, b):
905900
----------
906901
b : bool
907902
"""
908-
self._drawFrame = b
903+
self.legendPatch.set_visible(b)
909904
self.stale = True
910905

911906
draw_frame = set_frame_on # Backcompat alias.

lib/matplotlib/offsetbox.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -592,24 +592,19 @@ def __init__(self, child, pad=None, draw_frame=False, patch_attrs=None):
592592
Additional parameters passed to the contained `.FancyBboxPatch`.
593593
"""
594594
super().__init__()
595-
596595
self.pad = pad
597596
self._children = [child]
598-
599597
self.patch = FancyBboxPatch(
600598
xy=(0.0, 0.0), width=1., height=1.,
601599
facecolor='w', edgecolor='k',
602600
mutation_scale=1, # self.prop.get_size_in_points(),
603-
snap=True
604-
)
605-
606-
self.patch.set_boxstyle("square", pad=0)
607-
601+
snap=True,
602+
visible=draw_frame,
603+
boxstyle="square,pad=0",
604+
)
608605
if patch_attrs is not None:
609606
self.patch.update(patch_attrs)
610607

611-
self._drawFrame = draw_frame
612-
613608
def get_extent_offsets(self, renderer):
614609
# docstring inherited.
615610
dpicor = renderer.points_to_pixels(1.)
@@ -637,20 +632,15 @@ def draw(self, renderer):
637632
self.stale = False
638633

639634
def update_frame(self, bbox, fontsize=None):
640-
self.patch.set_bounds(bbox.x0, bbox.y0,
641-
bbox.width, bbox.height)
642-
635+
self.patch.set_bounds(bbox.x0, bbox.y0, bbox.width, bbox.height)
643636
if fontsize:
644637
self.patch.set_mutation_scale(fontsize)
645638
self.stale = True
646639

647640
def draw_frame(self, renderer):
648641
# update the location and size of the legend
649-
bbox = self.get_window_extent(renderer)
650-
self.update_frame(bbox)
651-
652-
if self._drawFrame:
653-
self.patch.draw(renderer)
642+
self.update_frame(self.get_window_extent(renderer))
643+
self.patch.draw(renderer)
654644

655645

656646
class DrawingArea(OffsetBox):
@@ -1110,10 +1100,10 @@ def __init__(self, loc,
11101100
xy=(0.0, 0.0), width=1., height=1.,
11111101
facecolor='w', edgecolor='k',
11121102
mutation_scale=self.prop.get_size_in_points(),
1113-
snap=True
1114-
)
1115-
self.patch.set_boxstyle("square", pad=0)
1116-
self._drawFrame = frameon
1103+
snap=True,
1104+
visible=frameon,
1105+
boxstyle="square,pad=0",
1106+
)
11171107

11181108
def set_child(self, child):
11191109
"""Set the child to be anchored."""
@@ -1210,26 +1200,22 @@ def _offset(w, h, xd, yd, renderer, fontsize=fontsize, self=self):
12101200
self.set_offset(_offset)
12111201

12121202
def update_frame(self, bbox, fontsize=None):
1213-
self.patch.set_bounds(bbox.x0, bbox.y0,
1214-
bbox.width, bbox.height)
1215-
1203+
self.patch.set_bounds(bbox.x0, bbox.y0, bbox.width, bbox.height)
12161204
if fontsize:
12171205
self.patch.set_mutation_scale(fontsize)
12181206

12191207
def draw(self, renderer):
12201208
# docstring inherited
1221-
12221209
if not self.get_visible():
12231210
return
12241211

12251212
fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
12261213
self._update_offset_func(renderer, fontsize)
12271214

1228-
if self._drawFrame:
1229-
# update the location and size of the legend
1230-
bbox = self.get_window_extent(renderer)
1231-
self.update_frame(bbox, fontsize)
1232-
self.patch.draw(renderer)
1215+
# update the location and size of the legend
1216+
bbox = self.get_window_extent(renderer)
1217+
self.update_frame(bbox, fontsize)
1218+
self.patch.draw(renderer)
12331219

12341220
width, height, xdescent, ydescent = self.get_extent(renderer)
12351221

@@ -1512,12 +1498,12 @@ def __init__(self, offsetbox, xy,
15121498
xy=(0.0, 0.0), width=1., height=1.,
15131499
facecolor='w', edgecolor='k',
15141500
mutation_scale=self.prop.get_size_in_points(),
1515-
snap=True
1516-
)
1501+
snap=True,
1502+
visible=frameon,
1503+
)
15171504
self.patch.set_boxstyle("square", pad=pad)
15181505
if bboxprops:
15191506
self.patch.set(**bboxprops)
1520-
self._drawFrame = frameon
15211507

15221508
@property
15231509
def xyann(self):
@@ -1670,9 +1656,7 @@ def draw(self, renderer):
16701656
self.arrow_patch.figure = self.figure
16711657
self.arrow_patch.draw(renderer)
16721658

1673-
if self._drawFrame:
1674-
self.patch.draw(renderer)
1675-
1659+
self.patch.draw(renderer)
16761660
self.offsetbox.draw(renderer)
16771661
self.stale = False
16781662

0 commit comments

Comments
 (0)