Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0772a65

Browse files
committed
[Doc] Document legend_handler and legend_handlers
1 parentf6e449b commit0772a65

File tree

4 files changed

+71
-21
lines changed

4 files changed

+71
-21
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``legend.legendHandles``
2+
~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
... was undocumented and has been renamed to ``legend_handles``. Using the
5+
old name is deprecated.

‎lib/matplotlib/legend.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,19 @@ def __init__(
360360
labels : list of str
361361
A list of labels to show next to the artists. The length of handles
362362
and labels should be the same. If they are not, they are truncated
363-
to thesmaller of both lengths.
363+
to theshortest of both lengths.
364364
365365
Other Parameters
366366
----------------
367367
%(_legend_kw_doc)s
368368
369+
Attributes
370+
----------
371+
legend_handles
372+
A list of `.legend_handlers` used by the legend.
373+
374+
.. versionadded:: 3.7
375+
369376
Notes
370377
-----
371378
Users can specify any arbitrary location for the legend using the
@@ -397,7 +404,7 @@ def __init__(
397404
self._fontsize=self.prop.get_size_in_points()
398405

399406
self.texts= []
400-
self.legendHandles= []
407+
self.legend_handles= []
401408
self._legend_title_box=None
402409

403410
#: A dictionary with the extra handler mappings for this Legend
@@ -561,7 +568,7 @@ def val_or_rc(val, rc_name):
561568
labelcolor=mpl.rcParams['text.color']
562569
ifisinstance(labelcolor,str)andlabelcolorincolor_getters:
563570
getter_names=color_getters[labelcolor]
564-
forhandle,textinzip(self.legendHandles,self.texts):
571+
forhandle,textinzip(self.legend_handles,self.texts):
565572
try:
566573
ifhandle.get_array()isnotNone:
567574
continue
@@ -594,6 +601,9 @@ def val_or_rc(val, rc_name):
594601
else:
595602
raiseValueError(f"Invalid labelcolor:{labelcolor!r}")
596603

604+
legendHandles=_api.deprecated('3.7',alternative="legend_handles")(
605+
property(lambdaself:self.legend_handles))
606+
597607
def_set_artist_props(self,a):
598608
"""
599609
Set the boilerplate props for artists added to axes.
@@ -838,7 +848,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
838848
self._legend_box.set_figure(self.figure)
839849
self._legend_box.axes=self.axes
840850
self.texts=text_list
841-
self.legendHandles=handle_list
851+
self.legend_handles=handle_list
842852

843853
def_auto_legend_data(self):
844854
"""
@@ -883,12 +893,12 @@ def get_frame(self):
883893

884894
defget_lines(self):
885895
r"""Return the list of `~.lines.Line2D`\s in the legend."""
886-
return [hforhinself.legendHandlesifisinstance(h,Line2D)]
896+
return [hforhinself.legend_handlesifisinstance(h,Line2D)]
887897

888898
defget_patches(self):
889899
r"""Return the list of `~.patches.Patch`\s in the legend."""
890900
returnsilent_list('Patch',
891-
[hforhinself.legendHandles
901+
[hforhinself.legend_handles
892902
ifisinstance(h,Patch)])
893903

894904
defget_texts(self):

‎lib/matplotlib/legend_handler.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
</tutorials/intermediate/legend_guide>` before reading this documentation.
1010
1111
Legend handlers are expected to be a callable object with a following
12-
signature.::
12+
signature::
1313
1414
legend_handler(legend, orig_handle, fontsize, handlebox)
1515
1616
Where *legend* is the legend itself, *orig_handle* is the original
17-
plot, *fontsize* is the fontsize in pixels, and *handlebox* isa
18-
OffsetBox instance. Within the call, you should create relevant
17+
plot, *fontsize* is the fontsize in pixels, and *handlebox* isan
18+
`.OffsetBox` instance. Within the call, you should create relevant
1919
artists (using relevant properties from the *legend* and/or
20-
*orig_handle*) and add them into the handlebox. The artists need to
21-
be scaled according to the fontsize (note that the size is in pixel,
20+
*orig_handle*) and add them into the*handlebox*. The artists need to
21+
be scaled according to the*fontsize* (note that the size is in pixel,
2222
i.e., this is dpi-scaled value).
2323
2424
This module includes definition of several legend handler classes
@@ -49,7 +49,7 @@ class HandlerBase:
4949
A base class for default legend handlers.
5050
5151
The derived classes are meant to override *create_artists* method, which
52-
hasa following signature.::
52+
hasthe following signature::
5353
5454
def create_artists(self, legend, orig_handle,
5555
xdescent, ydescent, width, height, fontsize,
@@ -61,6 +61,18 @@ def create_artists(self, legend, orig_handle,
6161
6262
"""
6363
def__init__(self,xpad=0.,ypad=0.,update_func=None):
64+
"""
65+
Parameters
66+
----------
67+
68+
xpad : float, optional
69+
Padding in x-direction.
70+
ypad : float, optional
71+
Padding in y-direction.
72+
update_func : callable, optional
73+
Function for updating the legend handler properties from another
74+
legend handler, used by `update_prop`.
75+
"""
6476
self._xpad,self._ypad=xpad,ypad
6577
self._update_prop_func=update_func
6678

@@ -133,6 +145,24 @@ def legend_artist(self, legend, orig_handle,
133145
defcreate_artists(self,legend,orig_handle,
134146
xdescent,ydescent,width,height,fontsize,
135147
trans):
148+
"""
149+
Return the legend artists generated.
150+
151+
Parameters
152+
----------
153+
legend : `~matplotlib.legend.Legend`
154+
The legend for which these legend artists are being created.
155+
orig_handle : `~matplotlib.artist.Artist` or similar
156+
The object for which these legend artists are being created.
157+
xdescent, ydescent, width, height : int
158+
The rectangle (*xdescent*, *ydescent*, *width*, *height*) that the
159+
legend artists being created should fit within.
160+
fontsize : int
161+
The fontsize in pixels. The legend artists being created should
162+
be scaled according to the given fontsize.
163+
trans : `~matplotlib.transforms.Transform`
164+
The transform that is applied to the legend artists being created.
165+
"""
136166
raiseNotImplementedError('Derived must override')
137167

138168

@@ -217,7 +247,7 @@ class HandlerLine2DCompound(HandlerNpoints):
217247
defcreate_artists(self,legend,orig_handle,
218248
xdescent,ydescent,width,height,fontsize,
219249
trans):
220-
250+
# Doc-string inherited
221251
xdata,xdata_marker=self.get_xdata(legend,xdescent,ydescent,
222252
width,height,fontsize)
223253

@@ -276,7 +306,7 @@ class HandlerLine2D(HandlerNpoints):
276306
defcreate_artists(self,legend,orig_handle,
277307
xdescent,ydescent,width,height,fontsize,
278308
trans):
279-
309+
# Doc-string inherited
280310
xdata,xdata_marker=self.get_xdata(legend,xdescent,ydescent,
281311
width,height,fontsize)
282312

@@ -341,6 +371,7 @@ def _create_patch(self, legend, orig_handle,
341371

342372
defcreate_artists(self,legend,orig_handle,
343373
xdescent,ydescent,width,height,fontsize,trans):
374+
# Doc-string inherited
344375
p=self._create_patch(legend,orig_handle,
345376
xdescent,ydescent,width,height,fontsize)
346377
self.update_prop(p,orig_handle,legend)
@@ -374,6 +405,7 @@ def _create_line(orig_handle, width, height):
374405

375406
defcreate_artists(self,legend,orig_handle,
376407
xdescent,ydescent,width,height,fontsize,trans):
408+
# Doc-string inherited
377409
iforig_handle.get_fill()or (orig_handle.get_hatch()isnotNone):
378410
p=self._create_patch(orig_handle,xdescent,ydescent,width,
379411
height)
@@ -404,7 +436,7 @@ def _default_update_prop(self, legend_handle, orig_handle):
404436

405437
defcreate_artists(self,legend,orig_handle,
406438
xdescent,ydescent,width,height,fontsize,trans):
407-
439+
# Doc-string inherited
408440
xdata,xdata_marker=self.get_xdata(legend,xdescent,ydescent,
409441
width,height,fontsize)
410442
ydata=np.full_like(xdata, (height-ydescent)/2)
@@ -471,6 +503,7 @@ def create_collection(self, orig_handle, sizes, offsets, offset_transform):
471503
defcreate_artists(self,legend,orig_handle,
472504
xdescent,ydescent,width,height,fontsize,
473505
trans):
506+
# Doc-string inherited
474507
xdata,xdata_marker=self.get_xdata(legend,xdescent,ydescent,
475508
width,height,fontsize)
476509

@@ -534,7 +567,7 @@ def get_err_size(self, legend, xdescent, ydescent,
534567
defcreate_artists(self,legend,orig_handle,
535568
xdescent,ydescent,width,height,fontsize,
536569
trans):
537-
570+
# Doc-string inherited
538571
plotlines,caplines,barlinecols=orig_handle
539572

540573
xdata,xdata_marker=self.get_xdata(legend,xdescent,ydescent,
@@ -653,6 +686,7 @@ def get_ydata(self, legend, xdescent, ydescent, width, height, fontsize):
653686
defcreate_artists(self,legend,orig_handle,
654687
xdescent,ydescent,width,height,fontsize,
655688
trans):
689+
# Doc-string inherited
656690
markerline,stemlines,baseline=orig_handle
657691
# Check to see if the stemcontainer is storing lines as a list or a
658692
# LineCollection. Eventually using a list will be removed, and this
@@ -730,7 +764,7 @@ def __init__(self, ndivide=1, pad=None, **kwargs):
730764
defcreate_artists(self,legend,orig_handle,
731765
xdescent,ydescent,width,height,fontsize,
732766
trans):
733-
767+
# Doc-string inherited
734768
handler_map=legend.get_legend_handler_map()
735769

736770
ifself._ndivideisNone:
@@ -797,6 +831,7 @@ def get_first(prop_array):
797831

798832
defcreate_artists(self,legend,orig_handle,
799833
xdescent,ydescent,width,height,fontsize,trans):
834+
# Doc-string inherited
800835
p=Rectangle(xy=(-xdescent,-ydescent),
801836
width=width,height=height)
802837
self.update_prop(p,orig_handle,legend)

‎lib/matplotlib/tests/test_legend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_legend_label_with_leading_underscore():
9191
withpytest.warns(UserWarning,
9292
match=r"starts with '_'.*excluded from the legend."):
9393
legend=ax.legend(handles=[line])
94-
assertlen(legend.legendHandles)==0
94+
assertlen(legend.legend_handles)==0
9595

9696

9797
@image_comparison(['legend_labels_first.png'],remove_text=True)
@@ -494,7 +494,7 @@ def test_linecollection_scaled_dashes():
494494
ax.add_collection(lc3)
495495

496496
leg=ax.legend([lc1,lc2,lc3], ["line1","line2",'line 3'])
497-
h1,h2,h3=leg.legendHandles
497+
h1,h2,h3=leg.legend_handles
498498

499499
foroh,lhinzip((lc1,lc2,lc3), (h1,h2,h3)):
500500
assertoh.get_linestyles()[0]==lh._dash_pattern
@@ -914,7 +914,7 @@ def test_legend_draggable(draggable):
914914
deftest_alpha_handles():
915915
x,n,hh=plt.hist([1,2,3],alpha=0.25,label='data',color='red')
916916
legend=plt.legend()
917-
forlhinlegend.legendHandles:
917+
forlhinlegend.legend_handles:
918918
lh.set_alpha(1.0)
919919
assertlh.get_facecolor()[:-1]==hh[1].get_facecolor()[:-1]
920920
assertlh.get_edgecolor()[:-1]==hh[1].get_edgecolor()[:-1]
@@ -1046,7 +1046,7 @@ def test_handlerline2d():
10461046
ax.scatter([0,1], [0,1],marker="v")
10471047
handles= [mlines.Line2D([0], [0],marker="v")]
10481048
leg=ax.legend(handles, ["Aardvark"],numpoints=1)
1049-
asserthandles[0].get_marker()==leg.legendHandles[0].get_marker()
1049+
asserthandles[0].get_marker()==leg.legend_handles[0].get_marker()
10501050

10511051

10521052
deftest_subfigure_legend():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp