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

Skip to content

Commit 6aeeec7

Browse files
dstansbyNelleV
authored andcommitted
Add some legend handler documentation (#9575)
* Small fix in legend_guide * Add some docstrings to legend_handler.py * DOC use double quotes for docstring The convention is to use triple "double quotes", and not single quotes.
1 parent 7bd5d64 commit 6aeeec7

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

lib/matplotlib/legend_handler.py

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def adjust_drawing_area(self, legend, orig_handle,
9090
return xdescent, ydescent, width, height
9191

9292
def legend_artist(self, legend, orig_handle,
93-
fontsize, handlebox):
93+
fontsize, handlebox):
9494
"""
9595
Return the artist that this HandlerBase generates for the given
9696
original artist/handle.
@@ -134,6 +134,19 @@ def create_artists(self, legend, orig_handle,
134134

135135
class HandlerNpoints(HandlerBase):
136136
def __init__(self, marker_pad=0.3, numpoints=None, **kw):
137+
"""
138+
Parameters
139+
----------
140+
marker_pad : float
141+
Padding between points in legend entry
142+
143+
numpoints : int
144+
Number of points to show in legend entry
145+
146+
Notes
147+
-----
148+
Any other keyword arguments are given to `HandlerBase`
149+
"""
137150
HandlerBase.__init__(self, **kw)
138151

139152
self._numpoints = numpoints
@@ -160,9 +173,21 @@ def get_xdata(self, legend, xdescent, ydescent, width, height, fontsize):
160173
return xdata, xdata_marker
161174

162175

163-
164176
class HandlerNpointsYoffsets(HandlerNpoints):
165177
def __init__(self, numpoints=None, yoffsets=None, **kw):
178+
"""
179+
Parameters
180+
----------
181+
numpoints : int
182+
Number of points to show in legend entry
183+
184+
yoffsets : array of floats
185+
Length *numpoints* list of y offsets for each point in legend entry
186+
187+
Notes
188+
-----
189+
Any other keyword arguments are given to `HandlerNpoints`
190+
"""
166191
HandlerNpoints.__init__(self, numpoints=numpoints, **kw)
167192
self._yoffsets = yoffsets
168193

@@ -180,7 +205,21 @@ class HandlerLine2D(HandlerNpoints):
180205
Handler for Line2D instances.
181206
"""
182207
def __init__(self, marker_pad=0.3, numpoints=None, **kw):
183-
HandlerNpoints.__init__(self, marker_pad=marker_pad, numpoints=numpoints, **kw)
208+
"""
209+
Parameters
210+
----------
211+
marker_pad : float
212+
Padding between points in legend entry
213+
214+
numpoints : int
215+
Number of points to show in legend entry
216+
217+
Notes
218+
-----
219+
Any other keyword arguments are given to `HandlerNpoints`
220+
"""
221+
HandlerNpoints.__init__(self, marker_pad=marker_pad,
222+
numpoints=numpoints, **kw)
184223

185224
def create_artists(self, legend, orig_handle,
186225
xdescent, ydescent, width, height, fontsize,
@@ -396,10 +435,11 @@ def __init__(self, xerr_size=0.5, yerr_size=None,
396435
self._xerr_size = xerr_size
397436
self._yerr_size = yerr_size
398437

399-
HandlerLine2D.__init__(self, marker_pad=marker_pad, numpoints=numpoints,
400-
**kw)
438+
HandlerLine2D.__init__(self, marker_pad=marker_pad,
439+
numpoints=numpoints, **kw)
401440

402-
def get_err_size(self, legend, xdescent, ydescent, width, height, fontsize):
441+
def get_err_size(self, legend, xdescent, ydescent,
442+
width, height, fontsize):
403443
xerr_size = self._xerr_size * fontsize
404444

405445
if self._yerr_size is None:
@@ -421,7 +461,6 @@ def create_artists(self, legend, orig_handle,
421461
ydata = ((height - ydescent) / 2.) * np.ones(xdata.shape, float)
422462
legline = Line2D(xdata, ydata)
423463

424-
425464
xdata_marker = np.asarray(xdata_marker)
426465
ydata_marker = np.asarray(ydata[:len(xdata_marker)])
427466

@@ -501,10 +540,28 @@ def create_artists(self, legend, orig_handle,
501540

502541
class HandlerStem(HandlerNpointsYoffsets):
503542
"""
504-
Handler for Errorbars
543+
Handler for plots produced by `stem`
505544
"""
506545
def __init__(self, marker_pad=0.3, numpoints=None,
507546
bottom=None, yoffsets=None, **kw):
547+
"""
548+
Parameters
549+
----------
550+
marker_pad : float
551+
Padding between points in legend entry. Default is 0.3.
552+
553+
numpoints : int, optional
554+
Number of points to show in legend entry
555+
556+
bottom : float, optional
557+
558+
yoffsets : array of floats, optional
559+
Length *numpoints* list of y offsets for each point in legend entry
560+
561+
Notes
562+
-----
563+
Any other keyword arguments are given to `HandlerNpointsYoffsets`
564+
"""
508565

509566
HandlerNpointsYoffsets.__init__(self, marker_pad=marker_pad,
510567
numpoints=numpoints,

tutorials/intermediate/legend_guide.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
not all artists can be added to a legend, at which point a "proxy" will have
4747
to be created (see :ref:`proxy_legend_handles` for further details).
4848
49-
# For full control of what is being added to the legend, it is common to pass
50-
# the appropriate handles directly to :func:`legend`
49+
For full control of what is being added to the legend, it is common to pass
50+
the appropriate handles directly to :func:`legend`::
5151
5252
line_up, = plt.plot([1,2,3], label='Line 2')
5353
line_down, = plt.plot([3,2,1], label='Line 1')

0 commit comments

Comments
 (0)