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

Skip to content

Commit c176df4

Browse files
authored
Merge pull request #10139 from dstansby/doc-handler
DOC: Improve legend_handler docstrings
2 parents 8be183c + 34fe23c commit c176df4

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

lib/matplotlib/legend_handler.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
i.e., this is dpi-scaled value).
1919
2020
This module includes definition of several legend handler classes
21-
derived from the base class (HandlerBase) with the following method.
21+
derived from the base class (HandlerBase) with the following method::
2222
2323
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
2424
25-
2625
"""
2726
from __future__ import (absolute_import, division, print_function,
2827
unicode_literals)
@@ -133,19 +132,22 @@ def create_artists(self, legend, orig_handle,
133132

134133

135134
class HandlerNpoints(HandlerBase):
135+
"""
136+
A legend handler that shows *numpoints* points in the legend entry.
137+
"""
136138
def __init__(self, marker_pad=0.3, numpoints=None, **kw):
137139
"""
138140
Parameters
139141
----------
140142
marker_pad : float
141-
Padding between points in legend entry
143+
Padding between points in legend entry.
142144
143145
numpoints : int
144-
Number of points to show in legend entry
146+
Number of points to show in legend entry.
145147
146148
Notes
147149
-----
148-
Any other keyword arguments are given to `HandlerBase`
150+
Any other keyword arguments are given to `HandlerBase`.
149151
"""
150152
HandlerBase.__init__(self, **kw)
151153

@@ -174,19 +176,24 @@ def get_xdata(self, legend, xdescent, ydescent, width, height, fontsize):
174176

175177

176178
class HandlerNpointsYoffsets(HandlerNpoints):
179+
"""
180+
A legend handler that shows *numpoints* in the legend, and allows them to
181+
be individually offest in the y-direction.
182+
"""
177183
def __init__(self, numpoints=None, yoffsets=None, **kw):
178184
"""
179185
Parameters
180186
----------
181187
numpoints : int
182-
Number of points to show in legend entry
188+
Number of points to show in legend entry.
183189
184190
yoffsets : array of floats
185-
Length *numpoints* list of y offsets for each point in legend entry
191+
Length *numpoints* list of y offsets for each point in
192+
legend entry.
186193
187194
Notes
188195
-----
189-
Any other keyword arguments are given to `HandlerNpoints`
196+
Any other keyword arguments are given to `HandlerNpoints`.
190197
"""
191198
HandlerNpoints.__init__(self, numpoints=numpoints, **kw)
192199
self._yoffsets = yoffsets
@@ -202,21 +209,21 @@ def get_ydata(self, legend, xdescent, ydescent, width, height, fontsize):
202209

203210
class HandlerLine2D(HandlerNpoints):
204211
"""
205-
Handler for Line2D instances.
212+
Handler for `.Line2D` instances.
206213
"""
207214
def __init__(self, marker_pad=0.3, numpoints=None, **kw):
208215
"""
209216
Parameters
210217
----------
211218
marker_pad : float
212-
Padding between points in legend entry
219+
Padding between points in legend entry.
213220
214221
numpoints : int
215-
Number of points to show in legend entry
222+
Number of points to show in legend entry.
216223
217224
Notes
218225
-----
219-
Any other keyword arguments are given to `HandlerNpoints`
226+
Any other keyword arguments are given to `HandlerNpoints`.
220227
"""
221228
HandlerNpoints.__init__(self, marker_pad=marker_pad,
222229
numpoints=numpoints, **kw)
@@ -254,21 +261,26 @@ def create_artists(self, legend, orig_handle,
254261

255262
class HandlerPatch(HandlerBase):
256263
"""
257-
Handler for Patch instances.
264+
Handler for `.Patch` instances.
258265
"""
259266
def __init__(self, patch_func=None, **kw):
260267
"""
261-
The HandlerPatch class optionally takes a function ``patch_func``
262-
who's responsibility is to create the legend key artist. The
263-
``patch_func`` should have the signature::
268+
Parameters
269+
----------
270+
patch_func : callable, optional
271+
The function that creates the legend key artist.
272+
*patch_func* should have the signature::
264273
265-
def patch_func(legend=legend, orig_handle=orig_handle,
266-
xdescent=xdescent, ydescent=ydescent,
267-
width=width, height=height, fontsize=fontsize)
274+
def patch_func(legend=legend, orig_handle=orig_handle,
275+
xdescent=xdescent, ydescent=ydescent,
276+
width=width, height=height, fontsize=fontsize)
268277
269-
Subsequently the created artist will have its ``update_prop`` method
270-
called and the appropriate transform will be applied.
278+
Subsequently the created artist will have its ``update_prop`` method
279+
called and the appropriate transform will be applied.
271280
281+
Notes
282+
-----
283+
Any other keyword arguments are given to `HandlerBase`.
272284
"""
273285
HandlerBase.__init__(self, **kw)
274286
self._patch_func = patch_func
@@ -295,7 +307,7 @@ def create_artists(self, legend, orig_handle,
295307

296308
class HandlerLineCollection(HandlerLine2D):
297309
"""
298-
Handler for LineCollection instances.
310+
Handler for `.LineCollection` instances.
299311
"""
300312
def get_numpoints(self, legend):
301313
if self._numpoints is None:
@@ -327,7 +339,7 @@ def create_artists(self, legend, orig_handle,
327339

328340
class HandlerRegularPolyCollection(HandlerNpointsYoffsets):
329341
"""
330-
Handler for RegularPolyCollections.
342+
Handler for `.RegularPolyCollections`.
331343
"""
332344
def __init__(self, yoffsets=None, sizes=None, **kw):
333345
HandlerNpointsYoffsets.__init__(self, yoffsets=yoffsets, **kw)
@@ -341,7 +353,7 @@ def get_numpoints(self, legend):
341353
return self._numpoints
342354

343355
def get_sizes(self, legend, orig_handle,
344-
xdescent, ydescent, width, height, fontsize):
356+
xdescent, ydescent, width, height, fontsize):
345357
if self._sizes is None:
346358
handle_sizes = orig_handle.get_sizes()
347359
if not len(handle_sizes):
@@ -402,7 +414,7 @@ def create_artists(self, legend, orig_handle,
402414

403415
class HandlerPathCollection(HandlerRegularPolyCollection):
404416
"""
405-
Handler for PathCollections, which are used by scatter
417+
Handler for `.PathCollections`, which are used by `~.Axes.scatter`.
406418
"""
407419
def create_collection(self, orig_handle, sizes, offsets, transOffset):
408420
p = type(orig_handle)([orig_handle.get_paths()[0]],
@@ -415,7 +427,7 @@ def create_collection(self, orig_handle, sizes, offsets, transOffset):
415427

416428
class HandlerCircleCollection(HandlerRegularPolyCollection):
417429
"""
418-
Handler for CircleCollections
430+
Handler for `.CircleCollections`.
419431
"""
420432
def create_collection(self, orig_handle, sizes, offsets, transOffset):
421433
p = type(orig_handle)(sizes,
@@ -427,7 +439,7 @@ def create_collection(self, orig_handle, sizes, offsets, transOffset):
427439

428440
class HandlerErrorbar(HandlerLine2D):
429441
"""
430-
Handler for Errorbars
442+
Handler for Errorbars.
431443
"""
432444
def __init__(self, xerr_size=0.5, yerr_size=None,
433445
marker_pad=0.3, numpoints=None, **kw):
@@ -540,7 +552,7 @@ def create_artists(self, legend, orig_handle,
540552

541553
class HandlerStem(HandlerNpointsYoffsets):
542554
"""
543-
Handler for plots produced by `stem`
555+
Handler for plots produced by `~.Axes.stem`.
544556
"""
545557
def __init__(self, marker_pad=0.3, numpoints=None,
546558
bottom=None, yoffsets=None, **kw):
@@ -551,16 +563,17 @@ def __init__(self, marker_pad=0.3, numpoints=None,
551563
Padding between points in legend entry. Default is 0.3.
552564
553565
numpoints : int, optional
554-
Number of points to show in legend entry
566+
Number of points to show in legend entry.
555567
556568
bottom : float, optional
557569
558570
yoffsets : array of floats, optional
559-
Length *numpoints* list of y offsets for each point in legend entry
571+
Length *numpoints* list of y offsets for each point in
572+
legend entry.
560573
561574
Notes
562575
-----
563-
Any other keyword arguments are given to `HandlerNpointsYoffsets`
576+
Any other keyword arguments are given to `HandlerNpointsYoffsets`.
564577
"""
565578

566579
HandlerNpointsYoffsets.__init__(self, marker_pad=marker_pad,
@@ -628,18 +641,14 @@ class HandlerTuple(HandlerBase):
628641
629642
Parameters
630643
----------
631-
632644
ndivide : int, optional
633-
The number of sections to divide the legend area into. If None,
645+
The number of sections to divide the legend area into. If None,
634646
use the length of the input tuple. Default is 1.
635647
636648
637649
pad : float, optional
638-
If None, fall back to `legend.borderpad` as the default.
650+
If None, fall back to ``legend.borderpad`` as the default.
639651
In units of fraction of font size. Default is None.
640-
641-
642-
643652
"""
644653
def __init__(self, ndivide=1, pad=None, **kwargs):
645654

@@ -681,7 +690,7 @@ def create_artists(self, legend, orig_handle,
681690

682691
class HandlerPolyCollection(HandlerBase):
683692
"""
684-
Handler for PolyCollection used in fill_between and stackplot.
693+
Handler for `.PolyCollection` used in `~.Axes.fill_between` and `~.Axes.stackplot`.
685694
"""
686695
def _update_prop(self, legend_handle, orig_handle):
687696
def first_color(colors):
@@ -692,6 +701,7 @@ def first_color(colors):
692701
return colors[0]
693702
else:
694703
return "none"
704+
695705
def get_first(prop_array):
696706
if len(prop_array):
697707
return prop_array[0]

0 commit comments

Comments
 (0)