|
53 | 53 | For full control of what is being added to the legend, it is common to pass
|
54 | 54 | the appropriate handles directly to :func:`legend`::
|
55 | 55 |
|
56 |
| - line_up, = plt.plot([1, 2, 3], label='Line 2') |
57 |
| - line_down, = plt.plot([3, 2, 1], label='Line 1') |
58 |
| - plt.legend(handles=[line_up, line_down]) |
| 56 | + fig, ax = plt.subplots() |
| 57 | + line_up, = ax.plot([1, 2, 3], label='Line 2') |
| 58 | + line_down, = ax.plot([3, 2, 1], label='Line 1') |
| 59 | + ax.legend(handles=[line_up, line_down]) |
59 | 60 |
|
60 | 61 | In some cases, it is not possible to set the label of the handle, so it is
|
61 | 62 | possible to pass through the list of labels to :func:`legend`::
|
62 | 63 |
|
63 |
| - line_up, = plt.plot([1, 2, 3], label='Line 2') |
64 |
| - line_down, = plt.plot([3, 2, 1], label='Line 1') |
65 |
| - plt.legend([line_up, line_down], ['Line Up', 'Line Down']) |
| 64 | + fig, ax = plt.subplots() |
| 65 | + line_up, = ax.plot([1, 2, 3], label='Line 2') |
| 66 | + line_down, = ax.plot([3, 2, 1], label='Line 1') |
| 67 | + ax.legend([line_up, line_down], ['Line Up', 'Line Down']) |
66 | 68 |
|
67 | 69 |
|
68 | 70 | .. _proxy_legend_handles:
|
|
81 | 83 | import matplotlib.patches as mpatches
|
82 | 84 | import matplotlib.pyplot as plt
|
83 | 85 |
|
| 86 | +fig, ax = plt.subplots() |
84 | 87 | red_patch = mpatches.Patch(color='red', label='The red data')
|
85 |
| -plt.legend(handles=[red_patch]) |
| 88 | +ax.legend(handles=[red_patch]) |
86 | 89 |
|
87 | 90 | plt.show()
|
88 | 91 |
|
|
92 | 95 |
|
93 | 96 | import matplotlib.lines as mlines
|
94 | 97 |
|
| 98 | +fig, ax = plt.subplots() |
95 | 99 | blue_line = mlines.Line2D([], [], color='blue', marker='*',
|
96 | 100 | markersize=15, label='Blue stars')
|
97 |
| -plt.legend(handles=[blue_line]) |
| 101 | +ax.legend(handles=[blue_line]) |
98 | 102 |
|
99 | 103 | plt.show()
|
100 | 104 |
|
|
110 | 114 | # figure's top right-hand corner instead of the axes' corner, simply specify
|
111 | 115 | # the corner's location and the coordinate system of that location::
|
112 | 116 | #
|
113 |
| -# plt.legend(bbox_to_anchor=(1, 1), |
114 |
| -# bbox_transform=plt.gcf().transFigure) |
| 117 | +# ax.legend(bbox_to_anchor=(1, 1), |
| 118 | +# bbox_transform=fig.transFigure) |
115 | 119 | #
|
116 | 120 | # More examples of custom legend placement:
|
117 | 121 |
|
118 |
| -plt.subplot(211) |
119 |
| -plt.plot([1, 2, 3], label="test1") |
120 |
| -plt.plot([3, 2, 1], label="test2") |
121 |
| - |
| 122 | +fig, ax_dict = plt.subplot_mosaic([['top', 'top'], ['bottom', 'BLANK']], |
| 123 | + empty_sentinel="BLANK") |
| 124 | +ax_dict['top'].plot([1, 2, 3], label="test1") |
| 125 | +ax_dict['top'].plot([3, 2, 1], label="test2") |
122 | 126 | # Place a legend above this subplot, expanding itself to
|
123 | 127 | # fully use the given bounding box.
|
124 |
| -plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left', |
125 |
| - ncol=2, mode="expand", borderaxespad=0.) |
| 128 | +ax_dict['top'].legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left', |
| 129 | + ncol=2, mode="expand", borderaxespad=0.) |
126 | 130 |
|
127 |
| -plt.subplot(223) |
128 |
| -plt.plot([1, 2, 3], label="test1") |
129 |
| -plt.plot([3, 2, 1], label="test2") |
| 131 | +ax_dict['bottom'].plot([1, 2, 3], label="test1") |
| 132 | +ax_dict['bottom'].plot([3, 2, 1], label="test2") |
130 | 133 | # Place a legend to the right of this smaller subplot.
|
131 |
| -plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.) |
| 134 | +ax_dict['bottom'].legend(bbox_to_anchor=(1.05, 1), |
| 135 | + loc='upper left', borderaxespad=0.) |
132 | 136 |
|
133 | 137 | plt.show()
|
134 | 138 |
|
|
144 | 148 | # handles on the Axes. To keep old legend instances, we must add them
|
145 | 149 | # manually to the Axes:
|
146 | 150 |
|
147 |
| -line1, = plt.plot([1, 2, 3], label="Line 1", linestyle='--') |
148 |
| -line2, = plt.plot([3, 2, 1], label="Line 2", linewidth=4) |
| 151 | +fig, ax = plt.subplots() |
| 152 | +line1, = ax.plot([1, 2, 3], label="Line 1", linestyle='--') |
| 153 | +line2, = ax.plot([3, 2, 1], label="Line 2", linewidth=4) |
149 | 154 |
|
150 | 155 | # Create a legend for the first line.
|
151 |
| -first_legend = plt.legend(handles=[line1], loc='upper right') |
| 156 | +first_legend = ax.legend(handles=[line1], loc='upper right') |
152 | 157 |
|
153 |
| -# Add the legend manually to the current Axes. |
154 |
| -plt.gca().add_artist(first_legend) |
| 158 | +# Add the legend manually to the Axes. |
| 159 | +ax.add_artist(first_legend) |
155 | 160 |
|
156 | 161 | # Create another legend for the second line.
|
157 |
| -plt.legend(handles=[line2], loc='lower right') |
| 162 | +ax.legend(handles=[line2], loc='lower right') |
158 | 163 |
|
159 | 164 | plt.show()
|
160 | 165 |
|
|
188 | 193 |
|
189 | 194 | from matplotlib.legend_handler import HandlerLine2D
|
190 | 195 |
|
191 |
| -line1, = plt.plot([3, 2, 1], marker='o', label='Line 1') |
192 |
| -line2, = plt.plot([1, 2, 3], marker='o', label='Line 2') |
| 196 | +fig, ax = plt.subplots() |
| 197 | +line1, = ax.plot([3, 2, 1], marker='o', label='Line 1') |
| 198 | +line2, = ax.plot([1, 2, 3], marker='o', label='Line 2') |
193 | 199 |
|
194 |
| -plt.legend(handler_map={line1: HandlerLine2D(numpoints=4)}) |
| 200 | +ax.legend(handler_map={line1: HandlerLine2D(numpoints=4)}) |
195 | 201 |
|
196 | 202 | ###############################################################################
|
197 | 203 | # As you can see, "Line 1" now has 4 marker points, where "Line 2" has 2 (the
|
|
208 | 214 |
|
209 | 215 | z = randn(10)
|
210 | 216 |
|
211 |
| -red_dot, = plt.plot(z, "ro", markersize=15) |
| 217 | +fig, ax = plt.subplots() |
| 218 | +red_dot, = ax.plot(z, "ro", markersize=15) |
212 | 219 | # Put a white cross over some of the data.
|
213 |
| -white_cross, = plt.plot(z[:5], "w+", markeredgewidth=3, markersize=15) |
| 220 | +white_cross, = ax.plot(z[:5], "w+", markeredgewidth=3, markersize=15) |
214 | 221 |
|
215 |
| -plt.legend([red_dot, (red_dot, white_cross)], ["Attr A", "Attr A+B"]) |
| 222 | +ax.legend([red_dot, (red_dot, white_cross)], ["Attr A", "Attr A+B"]) |
216 | 223 |
|
217 | 224 | ###############################################################################
|
218 | 225 | # The `.legend_handler.HandlerTuple` class can also be used to
|
219 | 226 | # assign several legend keys to the same entry:
|
220 | 227 |
|
221 | 228 | from matplotlib.legend_handler import HandlerLine2D, HandlerTuple
|
222 | 229 |
|
223 |
| -p1, = plt.plot([1, 2.5, 3], 'r-d') |
224 |
| -p2, = plt.plot([3, 2, 1], 'k-o') |
| 230 | +fig, ax = plt.subplots() |
| 231 | +p1, = ax.plot([1, 2.5, 3], 'r-d') |
| 232 | +p2, = ax.plot([3, 2, 1], 'k-o') |
225 | 233 |
|
226 |
| -l = plt.legend([(p1, p2)], ['Two keys'], numpoints=1, |
227 |
| - handler_map={tuple: HandlerTuple(ndivide=None)}) |
| 234 | +l = ax.legend([(p1, p2)], ['Two keys'], numpoints=1, |
| 235 | + handler_map={tuple: HandlerTuple(ndivide=None)}) |
228 | 236 |
|
229 | 237 | ###############################################################################
|
230 | 238 | # Implementing a custom legend handler
|
@@ -253,9 +261,10 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox):
|
253 | 261 | handlebox.add_artist(patch)
|
254 | 262 | return patch
|
255 | 263 |
|
| 264 | +fig, ax = plt.subplots() |
256 | 265 |
|
257 |
| -plt.legend([AnyObject()], ['My first handler'], |
258 |
| - handler_map={AnyObject: AnyObjectHandler()}) |
| 266 | +ax.legend([AnyObject()], ['My first handler'], |
| 267 | + handler_map={AnyObject: AnyObjectHandler()}) |
259 | 268 |
|
260 | 269 | ###############################################################################
|
261 | 270 | # Alternatively, had we wanted to globally accept ``AnyObject`` instances
|
@@ -286,7 +295,9 @@ def create_artists(self, legend, orig_handle,
|
286 | 295 |
|
287 | 296 | c = mpatches.Circle((0.5, 0.5), 0.25, facecolor="green",
|
288 | 297 | edgecolor="red", linewidth=3)
|
289 |
| -plt.gca().add_patch(c) |
290 | 298 |
|
291 |
| -plt.legend([c], ["An ellipse, not a rectangle"], |
292 |
| - handler_map={mpatches.Circle: HandlerEllipse()}) |
| 299 | +fig, ax = plt.subplots() |
| 300 | + |
| 301 | +ax.add_patch(c) |
| 302 | +ax.legend([c], ["An ellipse, not a rectangle"], |
| 303 | + handler_map={mpatches.Circle: HandlerEllipse()}) |
0 commit comments