rL itdZddlmZddlZddlmZddlmZddl m Z ddl m Z dZGdd ZGd d eZGd d eZGddeZGddeZGddeZGddeZGddeZGddeZGddeZGddeZGddeZGd d!eZGd"d#eZGd$d%eZy)&a Default legend handlers. .. important:: This is a low-level legend API, which most end users do not need. We recommend that you are familiar with the :ref:`legend guide ` before reading this documentation. Legend handlers are expected to be a callable object with a following signature:: legend_handler(legend, orig_handle, fontsize, handlebox) Where *legend* is the legend itself, *orig_handle* is the original plot, *fontsize* is the fontsize in pixels, and *handlebox* is an `.OffsetBox` instance. Within the call, you should create relevant artists (using relevant properties from the *legend* and/or *orig_handle*) and add them into the *handlebox*. The artists need to be scaled according to the *fontsize* (note that the size is in pixels, i.e., this is dpi-scaled value). This module includes definition of several legend handler classes derived from the base class (HandlerBase) with the following method:: def legend_artist(self, legend, orig_handle, fontsize, handlebox) )cycleN)cbook)Line2D) Rectanglecrtt|jd}||j|yyN)nextiter get_children update_from)tgtsrc first_childs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/matplotlib/legend_handler.pyupdate_from_first_childr(s3tC,,./6K  $c<eZdZdZd dZdZdZdZdZdZ d Z y) HandlerBasea A base class for default legend handlers. The derived classes are meant to override *create_artists* method, which has the following signature:: def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans): The overridden method needs to create artists of the given transform that fits in the given dimension (xdescent, ydescent, width, height) that are scaled by fontsize if necessary. Nc0||c|_|_||_y)ak Parameters ---------- xpad : float, optional Padding in x-direction. ypad : float, optional Padding in y-direction. update_func : callable, optional Function for updating the legend handler properties from another legend handler, used by `~HandlerBase.update_prop`. N)_xpad_ypad_update_prop_func)selfxpadypad update_funcs r__init__zHandlerBase.__init__>s"&t DJ!,rcf|j|j||y|j||yr)r_default_update_propr legend_handle orig_handles r _update_propzHandlerBase._update_propMs.  ! ! )  % %m[ A  " "=+ >rc&|j|yr)r r s rrz HandlerBase._default_update_propSs!!+.rc|j|||j||jd|jdyr)r#_set_artist_props set_clip_box set_clip_pathrr!r"legends r update_propzHandlerBase.update_propVs< -5  /""4(##D)rc||j|zz }||j|zz }||j|zz }||j|zz }||||fSr)rr)rr*r"xdescentydescentwidthheightfontsizes radjust_drawing_areazHandlerBase.adjust_drawing_area^s_djj833djj833 X--$**x//5&00rc |j|||j|j|j|j|\}}}}|j ||||||||j } | D]} |j| | dS)a. Return the artist that this HandlerBase generates for the given original artist/handle. Parameters ---------- legend : `~matplotlib.legend.Legend` The legend for which these legend artists are being created. orig_handle : :class:`matplotlib.artist.Artist` or similar The object for which these legend artists are being created. fontsize : int The fontsize in pixels. The artists being created should be scaled according to the given fontsize. handlebox : `~matplotlib.offsetbox.OffsetBox` The box which has been created to hold this legend entry's artists. Artists created in the `legend_artist` method must be added to this handlebox inside this method. r)r2r-r.r/r0create_artists get_transform add_artist) rr*r"r1 handleboxr-r.r/r0artistsas r legend_artistzHandlerBase.legend_artistgs*-1,D,D##Y%7%7)"2"2 -)(E6 %%fk&.%&. 0G0G0IK  $A   # $qzrc td)a Return the legend artists generated. Parameters ---------- legend : `~matplotlib.legend.Legend` The legend for which these legend artists are being created. orig_handle : `~matplotlib.artist.Artist` or similar The object for which these legend artists are being created. xdescent, ydescent, width, height : int The rectangle (*xdescent*, *ydescent*, *width*, *height*) that the legend artists being created should fit within. fontsize : int The fontsize in pixels. The legend artists being created should be scaled according to the given fontsize. trans : `~matplotlib.transforms.Transform` The transform that is applied to the legend artists being created. Typically from unit coordinates in the handler box to screen coordinates. zDerived must override)NotImplementedError) rr*r"r-r.r/r0r1transs rr4zHandlerBase.create_artistss.""9::r)r>N) __name__ __module__ __qualname____doc__rr#rr+r2r:r4rrrr.s+ -? /*1#J;rrc0eZdZdZdfd ZdZdZxZS)HandlerNpointszM A legend handler that shows *numpoints* points in the legend entry. c @t|di|||_||_y)a Parameters ---------- marker_pad : float Padding between points in legend entry. numpoints : int Number of points to show in legend entry. **kwargs Keyword arguments forwarded to `.HandlerBase`. NrC)superr _numpoints _marker_pad)r marker_pad numpointskwargs __class__s rrzHandlerNpoints.__init__s$ "6"#%rcJ|j |jS|jSr)rHrKrr*s r get_numpointszHandlerNpoints.get_numpointss" ?? "## #?? "rc|j|}|dkDr7|j|z}tj| |z| |z|z |} | } | | fS| | |zg} | d|zzg} | | fS)N?)rPrInplinspace) rr*r-r.r/r0r1rKpadxdata xdata_markers r get_xdatazHandlerNpoints.get_xdatas&&v. q=""X-CKK C!) E 1C 7 )+E!Ll""Y E 12E%Ie 34Ll""r)333333?N)r?r@rArBrrPrY __classcell__rMs@rrErEs& # #rrEc*eZdZdZdfd ZdZxZS)HandlerNpointsYoffsetsz A legend handler that shows *numpoints* in the legend, and allows them to be individually offset in the y-direction. c 6t|dd|i|||_y)aL Parameters ---------- numpoints : int Number of points to show in legend entry. yoffsets : array of floats Length *numpoints* list of y offsets for each point in legend entry. **kwargs Keyword arguments forwarded to `.HandlerNpoints`. rKNrC)rGr _yoffsets)rrKyoffsetsrLrMs rrzHandlerNpointsYoffsets.__init__s! 7977!rc|j||jz}|S|tj|jz}|Srr`_scatteryoffsetsrTasarrayrr*r-r.r/r0r1ydatas r get_ydataz HandlerNpointsYoffsets.get_ydatasA >> !V444E RZZ77E rNN)r?r@rArBrrhr[r\s@rr^r^s "rr^ceZdZdZdZy)HandlerLine2DCompoundz Original handler for `.Line2D` instances, that relies on combining a line-only with a marker-only artist. May be deprecated in the future. c :|j||||||\} } tj| ||z dz } t| | } |j | ||| j d| j dt| | dt| } |j | ||| jd|jdk7r.| j|jz}| j|| | _ | j|| j|| | gS)NdefaultNonerR)rYrT full_likerr+ set_drawstyle set_markerlen set_linestyle markerscaleget_markersizeset_markersize _legmarker set_transform)rr*r"r-r.r/r0r1r=rWrXrgleglinelegline_markernewszs rr4z$HandlerLine2DCompound.create_artistss#nnVXx-2FHF| Ufx&71%<>& +v6i(2 e4FS5F.GH f=$$V,    ""113f6H6HHE  ) )% 0,e$$$U+((rNr?r@rArBr4rCrrrkrks  )rrkceZdZdZdZy) HandlerLine2Dz Handler for `.Line2D` instances. See Also -------- HandlerLine2DCompound : An earlier handler implementation, which used one artist for the line and another for the marker(s). c |j||||||\} } d} |j|dk(r tj| d| dd} dg} tj| ||z dz } t | | | } |j | |||jdk7r.| j|jz}| j|| j|| gS)NrRrrm) markevery) rYrPrTrUrqrr+rvrwrxrz)rr*r"r-r.r/r0r1r=rWrXrrgr{r}s rr4zHandlerLine2D.create_artistss#nnVXx-2FHF|   f % *KKa%)Q7EI UVh%6!$;<; +v6    "**,v/A/AAE  " "5 )e$yrNr~rCrrrrs rrc0eZdZdZdfd ZdZdZxZS) HandlerPatchz) Handler for `.Patch` instances. c 2t|di|||_y)a Parameters ---------- patch_func : callable, optional The function that creates the legend key artist. *patch_func* should have the signature:: def patch_func(legend=legend, orig_handle=orig_handle, xdescent=xdescent, ydescent=ydescent, width=width, height=height, fontsize=fontsize) Subsequently, the created artist will have its ``update_prop`` method called and the appropriate transform will be applied. **kwargs Keyword arguments forwarded to `.HandlerBase`. NrC)rGr _patch_func)r patch_funcrLrMs rrzHandlerPatch.__init__?s$ "6"%rc v|jt| | f||}|S|j|||||||}|S)Nxyr/r0)r*r"r-r.r/r0r1)rr) rr*r"r-r.r/r0r1ps r _create_patchzHandlerPatch._create_patchTs^    #xi(3 %f6A   K*2X',Vh!PArc ~|j|||||||} |j| ||| j|| gSr)rr+rz rr*r"r-r.r/r0r1r=rs rr4zHandlerPatch.create_artists_sI   v{'5&( L K0 s rr)r?r@rArBrrr4r[r\s@rrr:s&* rrc6eZdZdZedZedZdZy)HandlerStepPatchzA Handler for `~.matplotlib.patches.StepPatch` instances. cDt| | f|||jS)N)rr/r0color)r get_facecolor)r"r-r.r/r0s rrzHandlerStepPatch._create_patchns,hY 2% &k.G.G.IK Krctd|g|dz |dz g|j|j|j}|j d|j d|S)Nrrm)r linestyle linewidthrnro)r get_edgecolor get_linestyle get_linewidthrrrs)r"r/r0r{s r _create_linezHandlerStepPatch._create_linessj!UfQhq%9*88:#.#<#<#>#.#<#<#> i(2rc |js|j)|j|||||} |j| ||n|j |||} | j || gSr)get_fill get_hatchrr+rrzrs rr4zHandlerStepPatch.create_artistssr    !k&;&;&=&I"";(E#)+A   Q V 4!!+uf=A s rN)r?r@rArB staticmethodrrr4rCrrrris6KK   rrc"eZdZdZdZdZdZy)HandlerLineCollectionz2 Handler for `.LineCollection` instances. cJ|j |jS|jSrrH scatterpointsrOs rrPz#HandlerLineCollection.get_numpoints" ?? "'' '?? "rc|jd}|jd}|jd}|j||j ||j |yNr)get_linewidths_us_linestyles get_colors set_colorru set_linewidth)rr!r"lwdashesrs rrz*HandlerLineCollection._default_update_propsb  ' ' )! ,++A.&&(+&##F+##B'rc |j||||||\} } tj| ||z dz } t| | } |j | ||| j || gS)Nrm)rYrTrqrr+rz) rr*r"r-r.r/r0r1r=rWrXrgr{s rr4z$HandlerLineCollection.create_artistsss#nnVXx-2FHF| UVh%6!$;<& +v6e$yrN)r?r@rArBrPrr4rCrrrrs# ( rrcBeZdZdZdfd ZdZdZdZdZdZ xZ S) HandlerRegularPolyCollectionz'Handler for `.RegularPolyCollection`\s.c 6t|dd|i|||_y)NrarC)rGr_sizes)rrasizesrLrMs rrz%HandlerRegularPolyCollection.__init__s 5(5f5 rcJ|j |jS|jSrrrOs rrPz*HandlerRegularPolyCollection.get_numpointsrrcr|j|j}t|sdg}t||jdzz} t ||jdzz} |j |} | dkrd| | zz| | gd| } | S| | z } | tjdd| z| z} | S|j} | S)NrRrmrSr) r get_sizesrtmaxrvminrPrTrU)rr*r"r-r.r/r0r1 handle_sizessize_maxsize_minrKrrngs rrz&HandlerRegularPolyCollection.get_sizess ;; &002L|$ !s <(6+=+=+BBH<(6+=+=+BBH**62I1}x(23X!##-I/  (*bkk!Q ::XE KKE rc|j|||j|jd|jd|j dy)NF)root)r# set_figure get_figurer'r(r)s rr+z(HandlerRegularPolyCollection.update_propsJ -5  !2!2!2!>?""4(##D)rcft||j|j|||S)N)rotationroffsetsoffset_transform)type get_numsides get_rotationrr"rrrs rcreate_collectionz.HandlerRegularPolyCollection.create_collections6 tK  $ $ & --/u.>  rc ,|j||||||\} } |j||||||} |j|||||||} |j|| t t | | |} |j | ||| j|| gSN)rr)rYrhrrlistzipr+set_offset_transform)rr*r"r-r.r/r0r1r=rWrXrgrrs rr4z+HandlerRegularPolyCollection.create_artistss#nnVXx-2FHF|vx$fh8v{Hh$fh8  " " \512U # L K0 u%s rri) r?r@rArBrrPrr+rr4r[r\s@rrrs$2 # ** rrceZdZdZdZy)HandlerPathCollectionzDHandler for `.PathCollection`\s, which are used by `~.Axes.scatter`.cPt||jdg|||S)Nr)rrr)r get_pathsrs rrz'HandlerPathCollection.create_collections2 tK  " " $Q ' (.>  rNr?r@rArBrrCrrrrs O rrceZdZdZdZy)HandlerCircleCollectionz"Handler for `.CircleCollection`\s.c*t||||Sr)rrs rrz)HandlerCircleCollection.create_collections  tK 75EG GrNrrCrrrrs -Grrc4eZdZdZ dfd ZdZdZxZS)HandlerErrorbarzHandler for Errorbars.c F||_||_t| d||d|y)N)rJrKrC) _xerr_size _yerr_sizerGr)r xerr_size yerr_sizerJrKrLrMs rrzHandlerErrorbar.__init__ s*$# NJ)NvNrcj|j|z}|j|}||fS|j|z}||fSr)rr) rr*r-r.r/r0r1rrs r get_err_sizezHandlerErrorbar.get_err_sizesGOOh. ?? "!I)##(2I)##rc ,|\} } } |j||||||\} } tj| ||z dz }t| |}tj| } tj|dt | }|j ||||||\}}t| |}| #|jd|jdn|j|| ||jd|jd|j|| ||jd|jdk7r.|j|jz}|j|g}g}|jrt!| |Dcgc]\}}||z |f||z|ff}}}t#j$|}|j|| d||j'|| rt| |z |}t| |z|}|j|| d||j|| d||jd|jd|j'||j'||j(rt!| |Dcgc]\}}|||z f|||zff}}}t#j$|}|j|| d||j'|| rt| ||z }t| ||z}|j|| d||j|| d||jd |jd |j'||j'|g||||}|D]}|j+||Scc}}wcc}}w) NrmFrnnonerprRr|_)rYrTrqrrertr set_visibler+rrrsrurvrwrxhas_xerrrmcollLineCollectionappendhas_yerrrz)rr*r"r-r.r/r0r1r= plotlinescaplines barlinecolsrWrXrgr{ ydata_markerrrr|r}handle_barlinecolshandle_caplinesxyvertscoll capline_left capline_rightr8artists rr4zHandlerErrorbar.create_artistss,7( 8["nnVXx-2FHF| UVh%6!$;<&zz,/ zz%(:\):";< #00816 J 9 l;      &  & &u -   Wi 8  ! !) ,   v &   ^Y ?  ( ( 0!!Q&&557&:L:LL--e4   !$\6 :  % %d +%lY&> M &|i'? N   x{FC   VD'',((-&&|4&&}5   !$\6 :  % %d +%lL94LM &|\I5M N   x{FC   VD'',((-&&|4&&}5  "1 3: > !cF$;$;;cABE RZZ77E rc \|\} } } t| tj} |j||||||\} }|j ||||||}|j d}n |j }t ||dt|}|j|| |t||Dcgc]\}}t ||g||g}}}| rFtj||j5|D]}|j|| | dddn't|| D]\}}|j|||t tj| tj| g||g}|j|| |g|||}|D]}|j!||Scc}}w#1swYzxYw)Nr>)r) isinstancerrrYrhrrrtr+rr _setattr_cm_copy_collection_propsrTrrrz)rr*r"r-r.r/r0r1r= markerline stemlinesbaselineusing_linecollrWrXrgrleg_markerlinerr leg_stemlineslinelmm leg_baseliner8rs rr4zHandlerStem.create_artistss+6' Ix$Iu/C/CD"nnVXx-2FHF|vx$fh8 << F\\F e4FS5F.GH V<&)u%=?!Q A 4? ? ""D,G,GI >)>D$$T9f=> > > ]I6 0A  Q/ 0rvve}bffUm<%v.0  x8@M@<@@ (F   ' (-?  > >s%F%F""F+c|j|jd|j|jdy)zt Copy properties from the `.LineCollection` *orig_handle* to the `.Line2D` *legend_handle*. rN)r get_colorrurr s rrz"HandlerStem._copy_collection_propss<  5 5 7 :;##K$=$=$?$BCr)rZNNN) r?r@rArBrrhr4rr[r\s@rrrts&26'+*.`Drrc*eZdZdZdfd ZdZxZS) HandlerTuplez Handler for Tuple. c @||_||_t| di|y)a Parameters ---------- ndivide : int or None, default: 1 The number of sections to divide the legend area into. If None, use the length of the input tuple. pad : float, default: :rc:`legend.borderpad` Padding in units of fraction of font size. **kwargs Keyword arguments forwarded to `.HandlerBase`. NrC)_ndivide_padrGr)rndividerVrLrMs rrzHandlerTuple.__init__s#    "6"rc |j} |j t|} n |j} |j|j|z} n|j|z} | dkDr|| | dz zz | z }t ||| zt j| zz } g} |D]F}|j| |}|j||t| |||||}| j|H| S)NrR) get_legend_handler_maprrtr borderpadrrTarangeget_legend_handlerr4r extend)rr*r"r-r.r/r0r1r= handler_maprrV xds_cyclea_listhandle1handler_a_lists rr4zHandlerTuple.create_artistss335 == +&GmmG 99 ""X-C))h&C Q;SGaK00G;E(eckRYYw5G%GGH " #G// WEG,,Y5&(EKG MM' "  # r)rRN)r?r@rArBrr4r[r\s@rrrs# rrceZdZdZdZdZy)HandlerPolyCollectionza Handler for `.PolyCollection` used in `~.Axes.fill_between` and `~.Axes.stackplot`. cd}d}||j|_||j|_|j|_|j |_|j |_|j|_ |j|_ |j||j|j||j|j||j!y)Nc>|jdk(ryt|dS)Nr)rrrr)sizetuple)colorss r first_colorz7HandlerPolyCollection._update_prop..first_color s {{a## #rc$t|r|dSyr)rt) prop_arrays r get_firstz5HandlerPolyCollection._update_prop..get_firsts:!!}$r)r _facecolorr _edgecolor_original_facecolor_original_edgecolorr_fillr_hatch _hatch_colorrrruget_linestylesrzget_transforms)rr!r"r)r,s rr#z"HandlerPolyCollection._update_prop s $  $/{/H/H/J#K  #.{/H/H/J#K  ,7,K,K ),7,K,K ))224 *446 %0%=%= "##Ik.H.H.J$KL##Ik.H.H.J$KL##Ik.H.H.J$KLrc tt| | f||} |j| ||| j|| gS)Nr)rr+rzrs rr4z$HandlerPolyCollection.create_artists%sC 8)hY/!& 2 K0 s rN)r?r@rArBr#r4rCrrr#r#sM8rr#)rB itertoolsrnumpyrT matplotlibrmatplotlib.linesrmatplotlib.patchesrmatplotlib.collections collectionsrrrrEr^rkrrrrrrrrrrr#rCrrr>s:#(&% u;u;p'#['#T^<")N")J$N$N,;,^"{"JM@F#9FR 8 G:GhmhVXD(XDv2;2j(K(r