Initialize the legend_box. The legend_box is an instance of the OffsetBox, which is packed with legend handles and texts. Once packed, their location is calculated during the drawing time.
(self, handles, labels, markerfirst=True)
| 867 | return None |
| 868 | |
| 869 | def _init_legend_box(self, handles, labels, markerfirst=True): |
| 870 | """ |
| 871 | Initialize the legend_box. The legend_box is an instance of |
| 872 | the OffsetBox, which is packed with legend handles and |
| 873 | texts. Once packed, their location is calculated during the |
| 874 | drawing time. |
| 875 | """ |
| 876 | |
| 877 | fontsize = self._fontsize |
| 878 | |
| 879 | # legend_box is a HPacker, horizontally packed with columns. |
| 880 | # Each column is a VPacker, vertically packed with legend items. |
| 881 | # Each legend item is a HPacker packed with: |
| 882 | # - handlebox: a DrawingArea which contains the legend handle. |
| 883 | # - labelbox: a TextArea which contains the legend text. |
| 884 | |
| 885 | text_list = [] # the list of text instances |
| 886 | handle_list = [] # the list of handle instances |
| 887 | handles_and_labels = [] |
| 888 | |
| 889 | # The approximate height and descent of text. These values are |
| 890 | # only used for plotting the legend handle. |
| 891 | descent = 0.35 * fontsize * (self.handleheight - 0.7) # heuristic. |
| 892 | height = fontsize * self.handleheight - descent |
| 893 | # each handle needs to be drawn inside a box of (x, y, w, h) = |
| 894 | # (0, -descent, width, height). And their coordinates should |
| 895 | # be given in the display coordinates. |
| 896 | |
| 897 | # The transformation of each handle will be automatically set |
| 898 | # to self.get_transform(). If the artist does not use its |
| 899 | # default transform (e.g., Collections), you need to |
| 900 | # manually set their transform to the self.get_transform(). |
| 901 | legend_handler_map = self.get_legend_handler_map() |
| 902 | |
| 903 | for orig_handle, label in zip(handles, labels): |
| 904 | handler = self.get_legend_handler(legend_handler_map, orig_handle) |
| 905 | if handler is None: |
| 906 | _api.warn_external( |
| 907 | "Legend does not support handles for " |
| 908 | f"{type(orig_handle).__name__} " |
| 909 | "instances.\nA proxy artist may be used " |
| 910 | "instead.\nSee: https://matplotlib.org/" |
| 911 | "stable/users/explain/axes/legend_guide.html" |
| 912 | "#controlling-the-legend-entries") |
| 913 | # No handle for this artist, so we just defer to None. |
| 914 | handle_list.append(None) |
| 915 | else: |
| 916 | textbox = TextArea(label, multilinebaseline=True, |
| 917 | textprops=dict( |
| 918 | verticalalignment='baseline', |
| 919 | horizontalalignment='left', |
| 920 | fontproperties=self.prop)) |
| 921 | handlebox = DrawingArea(width=self.handlelength * fontsize, |
| 922 | height=height, |
| 923 | xdescent=0., ydescent=descent) |
| 924 | |
| 925 | text_list.append(textbox._text) |
| 926 | # Create the artist for the legend which represents the |
no test coverage detected