|
34 | 34 | from matplotlib.font_manager import FontProperties
|
35 | 35 | from matplotlib.lines import Line2D
|
36 | 36 | from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch
|
37 |
| -from matplotlib.collections import (LineCollection, RegularPolyCollection, |
38 |
| - CircleCollection, PathCollection, |
39 |
| - PolyCollection) |
| 37 | +from matplotlib.collections import ( |
| 38 | + Collection, CircleCollection, LineCollection, PathCollection, |
| 39 | + PolyCollection, RegularPolyCollection) |
40 | 40 | from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
|
41 | 41 | from matplotlib.transforms import BboxTransformTo, BboxTransformFrom
|
42 | 42 |
|
@@ -831,18 +831,23 @@ def _auto_legend_data(self):
|
831 | 831 | List of (x, y) offsets of all collection.
|
832 | 832 | """
|
833 | 833 | assert self.isaxes # always holds, as this is only called internally
|
834 |
| - ax = self.parent |
835 |
| - lines = [line.get_transform().transform_path(line.get_path()) |
836 |
| - for line in ax.lines] |
837 |
| - bboxes = [patch.get_bbox().transformed(patch.get_data_transform()) |
838 |
| - if isinstance(patch, Rectangle) else |
839 |
| - patch.get_path().get_extents(patch.get_transform()) |
840 |
| - for patch in ax.patches] |
| 834 | + bboxes = [] |
| 835 | + lines = [] |
841 | 836 | offsets = []
|
842 |
| - for handle in ax.collections: |
843 |
| - _, transOffset, hoffsets, _ = handle._prepare_points() |
844 |
| - for offset in transOffset.transform(hoffsets): |
845 |
| - offsets.append(offset) |
| 837 | + for artist in self.parent._children: |
| 838 | + if isinstance(artist, Line2D): |
| 839 | + lines.append( |
| 840 | + artist.get_transform().transform_path(artist.get_path())) |
| 841 | + elif isinstance(artist, Rectangle): |
| 842 | + bboxes.append( |
| 843 | + artist.get_bbox().transformed(artist.get_data_transform())) |
| 844 | + elif isinstance(artist, Patch): |
| 845 | + bboxes.append( |
| 846 | + artist.get_path().get_extents(artist.get_transform())) |
| 847 | + elif isinstance(artist, Collection): |
| 848 | + _, transOffset, hoffsets, _ = artist._prepare_points() |
| 849 | + for offset in transOffset.transform(hoffsets): |
| 850 | + offsets.append(offset) |
846 | 851 | return bboxes, lines, offsets
|
847 | 852 |
|
848 | 853 | def get_children(self):
|
@@ -1119,13 +1124,17 @@ def _get_legend_handles(axs, legend_handler_map=None):
|
1119 | 1124 | """
|
1120 | 1125 | handles_original = []
|
1121 | 1126 | for ax in axs:
|
1122 |
| - handles_original += [*ax.lines, *ax.patches, *ax.collections, |
1123 |
| - *ax.containers] |
| 1127 | + handles_original += [ |
| 1128 | + *(a for a in ax._children |
| 1129 | + if isinstance(a, (Line2D, Patch, Collection))), |
| 1130 | + *ax.containers] |
1124 | 1131 | # support parasite axes:
|
1125 | 1132 | if hasattr(ax, 'parasites'):
|
1126 | 1133 | for axx in ax.parasites:
|
1127 |
| - handles_original += [*axx.lines, *axx.patches, |
1128 |
| - *axx.collections, *axx.containers] |
| 1134 | + handles_original += [ |
| 1135 | + *(a for a in axx._children |
| 1136 | + if isinstance(a, (Line2D, Patch, Collection))), |
| 1137 | + *axx.containers] |
1129 | 1138 |
|
1130 | 1139 | handler_map = Legend.get_default_handler_map()
|
1131 | 1140 |
|
|
0 commit comments