|
35 | 35 | from matplotlib.lines import Line2D
|
36 | 36 | from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch,
|
37 | 37 | StepPatch)
|
| 38 | +from matplotlib.text import Text |
38 | 39 | from matplotlib.collections import (
|
39 | 40 | Collection, CircleCollection, LineCollection, PathCollection,
|
40 | 41 | PolyCollection, RegularPolyCollection)
|
@@ -642,7 +643,7 @@ def draw(self, renderer):
|
642 | 643 | StemContainer: legend_handler.HandlerStem(),
|
643 | 644 | ErrorbarContainer: legend_handler.HandlerErrorbar(),
|
644 | 645 | Line2D: legend_handler.HandlerLine2D(),
|
645 |
| - Patch: legend_handler.HandlerPatch(), |
| 646 | + Rectangle: legend_handler.HandlerPatch(), |
646 | 647 | StepPatch: legend_handler.HandlerStepPatch(),
|
647 | 648 | LineCollection: legend_handler.HandlerLineCollection(),
|
648 | 649 | RegularPolyCollection: legend_handler.HandlerRegularPolyCollection(),
|
@@ -763,28 +764,35 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
|
763 | 764 | for orig_handle, lab in zip(handles, labels):
|
764 | 765 | handler = self.get_legend_handler(legend_handler_map, orig_handle)
|
765 | 766 | if handler is None:
|
766 |
| - _api.warn_external( |
767 |
| - "Legend does not support {!r} instances.\nA proxy artist " |
768 |
| - "may be used instead.\nSee: " |
769 |
| - "https://matplotlib.org/users/legend_guide.html" |
770 |
| - "#creating-artists-specifically-for-adding-to-the-legend-" |
771 |
| - "aka-proxy-artists".format(orig_handle)) |
772 |
| - # We don't have a handle for this artist, so we just defer |
773 |
| - # to None. |
774 |
| - handle_list.append(None) |
775 |
| - else: |
776 |
| - textbox = TextArea(lab, textprops=label_prop, |
777 |
| - multilinebaseline=True) |
778 |
| - handlebox = DrawingArea(width=self.handlelength * fontsize, |
779 |
| - height=height, |
780 |
| - xdescent=0., ydescent=descent) |
781 |
| - |
782 |
| - text_list.append(textbox._text) |
783 |
| - # Create the artist for the legend which represents the |
784 |
| - # original artist/handle. |
785 |
| - handle_list.append(handler.legend_artist(self, orig_handle, |
786 |
| - fontsize, handlebox)) |
787 |
| - handles_and_labels.append((handlebox, textbox)) |
| 767 | + if isinstance(orig_handle, Text): |
| 768 | + _api.warn_external( |
| 769 | + "Legend does not support handles for Text " |
| 770 | + "instances.\nSee: https://matplotlib.org/stable/" |
| 771 | + "tutorials/intermediate/legend_guide.html" |
| 772 | + "#implementing-a-custom-legend-handler") |
| 773 | + continue |
| 774 | + else: |
| 775 | + _api.warn_external( |
| 776 | + "Legend does not support handles for {0} " |
| 777 | + "instances.\nA Rectangle proxy artist will be " |
| 778 | + "used instead.\nSee: https://matplotlib.org/" |
| 779 | + "stable/tutorials/intermediate/legend_guide.html" |
| 780 | + "#controlling-the-legend-entries".format( |
| 781 | + type(orig_handle).__name__)) |
| 782 | + handler = self.get_legend_handler(legend_handler_map, |
| 783 | + Rectangle) |
| 784 | + textbox = TextArea(lab, textprops=label_prop, |
| 785 | +
10000
multilinebaseline=True) |
| 786 | + handlebox = DrawingArea(width=self.handlelength * fontsize, |
| 787 | + height=height, |
| 788 | + xdescent=0., ydescent=descent) |
| 789 | + |
| 790 | + text_list.append(textbox._text) |
| 791 | + # Create the artist for the legend which represents the |
| 792 | + # original artist/handle. |
| 793 | + handle_list.append(handler.legend_artist(self, orig_handle, |
| 794 | + fontsize, handlebox)) |
| 795 | + handles_and_labels.append((handlebox, textbox)) |
788 | 796 |
|
789 | 797 | if handles_and_labels:
|
790 | 798 | # We calculate number of rows in each column. The first
|
@@ -1141,27 +1149,19 @@ def _get_legend_handles(axs, legend_handler_map=None):
|
1141 | 1149 | for ax in axs:
|
1142 | 1150 | handles_original += [
|
1143 | 1151 | *(a for a in ax._children
|
1144 |
| - if isinstance(a, (Line2D, Patch, Collection))), |
| 1152 | + if isinstance(a, (Line2D, Patch, Collection, Text))), |
1145 | 1153 | *ax.containers]
|
1146 | 1154 | # support parasite axes:
|
1147 | 1155 | if hasattr(ax, 'parasites'):
|
1148 | 1156 | for axx in ax.parasites:
|
1149 | 1157 | handles_original += [
|
1150 | 1158 | *(a for a in axx._children
|
1151 |
| - if isinstance(a, (Line2D, Patch, Collection))), |
| 1159 | + if isinstance(a, (Line2D, Patch, Collection, Text))), |
1152 | 1160 | *axx.containers]
|
1153 | 1161 |
|
1154 |
| - handler_map = Legend.get_default_handler_map() |
1155 |
| - |
1156 |
| - if legend_handler_map is not None: |
1157 |
| - handler_map = handler_map.copy() |
1158 |
| - handler_map.update(legend_handler_map) |
1159 |
| - |
1160 |
| - has_handler = Legend.get_legend_handler |
1161 |
| - |
1162 | 1162 | for handle in handles_original:
|
1163 | 1163 | label = handle.get_label()
|
1164 |
| - if label != '_nolegend_' and has_handler(handler_map, handle): |
| 1164 | + if label not in ['_nolegend_', '']: |
1165 | 1165 | yield handle
|
1166 | 1166 |
|
1167 | 1167 |
|
|
0 commit comments