
    .iD                       d Z ddlmZ ddlmZ ddlZddlmZ ddl	Z
ddl	mZ ddlmZ erddlmZ ddlmZ dd	Zdd
Zd Zd dZd d!dZd Zd"dZd Zd#dZd$dZd"dZd%dZd&dZ	 ddZ d'dZ!d(dZ"d Z#d Z$d)dZ%d*dZ&d Z'd Z(y)+zF
Module consolidating common testing functions for checking plotting.
    )annotations)TYPE_CHECKINGN)is_list_like)Series)SequenceAxesc                    |r|t        d      t        |       } | D ]Q  }|r;|j                         J t        |j                         j	                         |       @|j                         QJ  y)a)  
    Check each axes has expected legend labels

    Parameters
    ----------
    axes : matplotlib Axes object, or its list-like
    labels : list-like
        expected legend labels
    visible : bool
        expected legend visibility. labels are checked only when visible is
        True
    Nz-labels must be specified when visible is True)
ValueError_flatten_visible
get_legend_check_text_labels	get_texts)axeslabelsvisibleaxs       Z/var/www/app/trading-bot/venv/lib/python3.12/site-packages/pandas/tests/plotting/common.py_check_legend_labelsr      sp     FNHIID!D +==?...r}}88:FC==?***+    c                    |r|t        d      |r8| j                         \  }}|D cg c]  }|j                          }}||k(  sJ y| j                         J yc c}w )a  
    Check ax has expected legend markers

    Parameters
    ----------
    ax : matplotlib Axes object
    expected_markers : list-like
        expected legend markers
    visible : bool
        expected legend visibility. labels are checked only when visible is
        True
    Nz.Markers must be specified when visible is True)r   get_legend_handles_labels
get_markerr   )r   expected_markersr   handles_handlemarkerss          r   _check_legend_markerr   /   sp     $,IJJ113
5<=66$$&==****}}&&& >s   Ac                   | j                         }|j                         }t        |      t        |      k(  sJ t        ||d      D ];  \  }}|j                         }|j                         }t	        j
                  ||       = y)z
    Check each axes has identical lines

    Parameters
    ----------
    xp : matplotlib Axes object
    rs : matplotlib Axes object
    TstrictN)	get_lineslenzip
get_xydatatmassert_almost_equal)xprsxp_linesrs_linesxplrslxpdatarsdatas           r   _check_datar1   F   st     ||~H||~Hx=CM)))(48 /S!!
vv./r   c                |    ddl m} t        | |      st        |       s| g} | D ]  }|j	                         |k(  rJ  y)z
    Check each artist is visible or not

    Parameters
    ----------
    collections : matplotlib Artist or its list-like
        target Artist or its list or collection
    visible : bool
        expected visibility
    r   )
CollectionN)matplotlib.collectionsr3   
isinstancer   get_visible)collectionsr   r3   patchs       r   _check_visibler9   Y   sE     2k:.|K7P"m .  "g---.r   c                l    t        |       } | D ]$  }|j                  D ]  }|j                  |k(  rJ  & y)z
    Check for each artist whether it is filled or not

    Parameters
    ----------
    axes : matplotlib Axes object, or its list-like
    filled : bool
        expected filling
    N)r   patchesfill)r   filledr   r8   s       r   _check_patches_all_filledr>   m   sB     D!D (ZZ 	(E::'''	((r   c                    | j                         }t        t        ||            }| j                  D cg c]  }||   	 c}S c c}w N)uniquedictr%   values)seriescolorsrA   mappedvs        r   _get_colors_mappedrH   ~   s<    ]]_F #ff%&F%}}-!F1I---s   Ac                Z   ddl m} ddlm}m}m} ddlm} |j                  }	||t        ||      }|dt        |        }t        |       t        |      k(  sJ t        | |d      D ]  \  }
}t        |
|      r"|
j                         }|	j                  |      }n;t        |
||f      rt        |
j!                         d         }n|
j!                         }|	j                  |      }||k(  rJ  ||t        ||      }|dt        |        }t        |       t        |      k(  sJ t        | |d      D ]r  \  }
}t        |
|      r|
j#                         d   }n|
j#                         }t        |t$        j&                        rt        |      }|	j                  |      }||k(  rrJ  yy)a  
    Check each artist has expected line colors and face colors

    Parameters
    ----------
    collections : list-like
        list or collection of target artist
    linecolors : list-like which has the same length as collections
        list of expected line colors
    facecolors : list-like which has the same length as collections
        list of expected face colors
    mapping : Series
        Series used for color grouping key
        used for andrew_curves, parallel_coordinates, radviz test
    r   )rE   )r3   LineCollectionPolyCollection)Line2DNTr!   )
matplotlibrE   r4   r3   rJ   rK   matplotlib.linesrL   ColorConverterrH   r$   r%   r5   	get_colorto_rgbatupleget_edgecolorget_facecolornpndarray)r7   
linecolors
facecolorsmappingrE   r3   rJ   rK   rL   convr8   colorresultexpecteds                 r   _check_colorsr^      s     " 
 (  D+GZ@J#$6c+&67J;3z?222ZE 	&LE5%(*f-ENN#CDu224Q78,,.||E*HX%%%	& +GZ@J#$6c+&67J;3z?222ZE 	&LE5%,,,.q1,,.&"**-v||E*HX%%%	& r   c                    t        |       s| j                         |k(  sJ y| D cg c]  }|j                          }}t        |      t        |      k(  sJ t        ||d      D ]  \  }}||k(  rJ  yc c}w )a  
    Check each text has expected labels

    Parameters
    ----------
    texts : matplotlib Text object, or its list-like
        target text, or its list
    expected : str or list-like which has the same length as texts
        expected text label, or its list
    Tr!   N)r   get_textr$   r%   )textsr]   tr   labeles         r   r   r      s}     ~~8+++(-.1!**,..6{c(m+++FHT: 	HE1A::	 /s   A6c                   ddl m} t        |       } | D ]d  }||t        |j                  j                         |      r|j                         }n#|j                         |j                  d      z   }|D ]O  }|$t        j                  |j                         |       |,t        j                  |j                         |       Q ||t        |j                  j                         |      r|j                         }n#|j                         |j                  d      z   }|D ]O  }|$t        j                  |j                         |       |,t        j                  |j                         |       Q g y)ac  
    Check each axes has expected tick properties

    Parameters
    ----------
    axes : matplotlib Axes object, or its list-like
    xlabelsize : number
        expected xticks font size
    xrot : number
        expected xticks rotation
    ylabelsize : number
        expected yticks font size
    yrot : number
        expected yticks rotation
    r   )NullFormatterNT)minor)matplotlib.tickerrf   r   r5   xaxisget_minor_formatterget_xticklabelsr'   r(   get_fontsizeget_rotationyaxisget_yticklabels)	r   
xlabelsizexrot
ylabelsizeyrotrf   r   r   rc   s	            r   _check_ticks_propsrt      sf     0D!D G!T%5"((668-H ++-++-0B0B0B0NN G)**5+=+=+?L#**5+=+=+?F	G !T%5"((668-H++-++-0B0B0B0NN G)**5+=+=+?L#**5+=+=+?F	G+Gr   c                    t        |       } | D ]@  }|j                  j                         |k(  sJ |j                  j                         |k(  r@J  y)z
    Check each axes has expected scales

    Parameters
    ----------
    axes : matplotlib Axes object, or its list-like
    xaxis : {'linear', 'log'}
        expected xaxis scale
    yaxis : {'linear', 'log'}
        expected yaxis scale
    N)r   ri   	get_scalern   )r   ri   rn   r   s       r   _check_ax_scalesrw   	  sS     D!D -xx!!#u,,,xx!!#u,,,-r   c                \   ddl m} |d}t        |       }|5t        |      |k(  sJ |D ]   }t        |j	                               dkD  r J  |t               }t               } ||       D ]N  }|j                         j                         }	|j                  |	d   d          |j                  |	d   d          P t        |      t        |      f}
|
|k(  sJ t        j                  |d   j                  j                         t        j                  |t        j                               y)a  
    Check expected number of axes is drawn in expected layout

    Parameters
    ----------
    axes : matplotlib Axes object, or its list-like
    axes_num : number
        expected number of axes. Unnecessary axes should be set to
        invisible.
    layout : tuple
        expected layout, (expected number of rows , columns)
    figsize : tuple
        expected figsize. default is matplotlib default
    r   flatten_axesN)g@g333333@   )dtype)!pandas.plotting._matplotlib.toolsrz   r   r$   get_childrensetget_position
get_pointsaddr'   assert_numpy_array_equalfigureget_size_inchesrU   arrayfloat64)r   axes_numlayoutfigsizerz   visible_axesr   x_sety_setpointsr\   s              r   _check_axes_shaper     s    ?#D)L< H,,, 	.Br()A---	. t$ 	$B__&113FIIfQil#IIfQil#		$
 e*c%j)Q..0


+r   c                l    ddl m}  ||       }|D cg c]  }|j                         s| } }| S c c}w )z
    Flatten axes, and filter only visible

    Parameters
    ----------
    axes : matplotlib Axes object, or its list-like

    r   ry   )r}   rz   r6   )r   rz   axes_ndarrayr   s       r   r   r   G  s7     ?%L%:2)9B:D:K ;s   11c                    t        |       } | D ]P  }|j                  }d}d}|D ]+  }t        |dd      }t        |dd      }	|r|dz  }|	s'|dz  }- ||k(  sJ ||k(  rPJ  y)z
    Check axes has expected number of errorbars

    Parameters
    ----------
    axes : matplotlib Axes object, or its list-like
    xerr : number
        expected number of x errorbar
    yerr : number
        expected number of y errorbar
    r   has_xerrFhas_yerrr{   N)r   
containersgetattr)
r   xerryerrr   r   
xerr_count
yerr_countcr   r   s
             r   _check_has_errorbarsr   W  s     D!D "]]


 	 Aq*e4Hq*e4Ha
a
	  z!!!z!!!"r   c                6   ddl m} t        |t        d}|P|d}t	        | ||         sJ |dk(  r5t	        | j
                  |      sJ t	        | j                  t              sJ yy|t        |       D ]  }t	        ||      rJ  yt	        | t              sJ t        | j                               t        |      k(  sJ | j                         D ]  \  }}t	        |||         sJ |dk(  r|s|j                         |k(  r3J |dk(  rV|r|j
                  j                         |k(  sJ t	        |j
                  |      sJ t	        |j                  t              rJ |dk(  r-|d   d   }	|	j                  }
|s|
j                         |k(  rJ t         y)	a<  
    Check box returned type is correct

    Parameters
    ----------
    returned : object to be tested, returned from boxplot
    return_type : str
        return_type passed to boxplot
    expected_keys : list-like, optional
        group labels in subplot case. If not passed,
        the function checks assuming boxplot uses single ax
    check_ax_title : bool
        Whether to check the ax.title is the same as expected_key
        Intended to be checked by calling from ``boxplot``.
        Normal ``plot`` doesn't attach ``ax.title``, it must be disabled.
    r   r   )rB   r   bothNrB   r   r   medians)matplotlib.axesr	   rB   rR   r5   r   linesr   r   sortedkeysitems	get_titler   AssertionError)returnedreturn_typeexpected_keyscheck_ax_titler	   typesrkeyvalueliner   s              r   _check_box_return_typer   s  s   & %47E K(E+$6777& hkk4000hnnd333 !
 %h/ +!!T***+(F+++hmmo&&*????"..* 	%JCeU;%7888f$! ??,333&! 88--/3666!%((D111!%++t444&Y'*yy!>>+s222$$#	%r   c                   dd l fd}|i }d}|D ]  }j                  j                  ddt        |      z  |       |dz  }j	                  dd        | j
                  dd|i|  |       rJ j                  j                          j                  j                  ddt        |      z  |       |dz  }j	                  dd	        | j
                  d|dd
|  |       rJ j                  j                          |dvsj                  j                  ddt        |      z  |       |dz  }j	                  dd	        | j
                  dd|i|  |       sJ j                  j                          j                  j                  ddt        |      z  |       |dz  }j	                  dd        | j
                  d|d	d
|  |       sJ j                  j                           y )Nr   c                 "   j                   j                         j                  j                         } j                   j                         j                  j                         }t        d | D              }t        d |D              }|xr | S )Nc              3  R   K   | ]  }|j                   j                           ! y wr@   gridliner6   .0gs     r   	<genexpr>z;_check_grid_settings.<locals>.is_grid_on.<locals>.<genexpr>        @Aqzz--//@   %'c              3  R   K   | ]  }|j                   j                           ! y wr@   r   r   s     r   r   z;_check_grid_settings.<locals>.is_grid_on.<locals>.<genexpr>  r   r   )pyplotgcari   get_major_ticksrn   all)xticksyticksxoffyoffmpls       r   
is_grid_onz(_check_grid_settings.<locals>.is_grid_on  sn    !''779!''779@@@@@@MT""r   r{      r   F)gridkindT)r   r   )piehexbinscatter )rM   r   subplotr$   rcplotclf)objkindskwsr   spndxr   r   s         @r   _check_grid_settingsr     s    # {E 

1a#e*ne4
vE""d"c"<



1a#e*ne4
vD!.d.#.<

33JJq!c%j.%8QJEFF6F%CHH&$&#&<<JJNNJJq!c%j.%8QJEFF6F&CHH1$T1S1<<JJNN9r   c                8    | d   D cg c]  }||   	 c}S c c}w )zL
    Auxiliary function for correctly unpacking cycler after MPL >= 1.5
    zaxes.prop_cycler   )rcParamsfieldrG   s      r   _unpack_cyclerr     s"     ''89:AeH:::s   c                     | j                   d   S )Nx_shared_axesr   s    r   
get_x_axisr         ??3r   c                     | j                   d   S )Nyr   r   s    r   
get_y_axisr     r   r   c                   ddl m} ddlm} t	        | t
        t        j                  f      rdt	        | t
              r| j                  } | j                  d      D ]3  }dt        |      j                  }t	        ||t        f      r.J |        y dt        |       j                  }t	        | |t        t        f      sJ |       y )Nr   )Artistr   zBone of 'objs' is not a matplotlib Axes instance, type encountered zoobjs is neither an ndarray of Artist instances nor a single ArtistArtist instance, tuple, or dict, 'objs' is a )matplotlib.artistr   r   r	   r5   r   rU   rV   _valuesreshapetype__name__rB   rR   )objsr   r	   elmsgs        r   "assert_is_valid_plot_return_objectr     s    ($$,-dF#<<D,,r" 	5B$$(H$5$5#8:  b4,/44/	5BDz""%' 	
 $ 56;;6r   c                    ddl m} |rt        }nt        }d}|j	                  d|j                               }|j                           || |fi |D ]  }t        |        |S )a  
    Create plot and ensure that plot return object is valid.

    Parameters
    ----------
    f : func
        Plotting function.
    default_axes : bool, optional
        If False (default):
            - If `ax` not in `kwargs`, then create subplot(211) and plot there
            - Create new subplot(212) and plot there as well
            - Mind special corner case for bootstrap_plot (see `_gen_two_subplots`)
        If True:
            - Simply run plotting function with kwargs provided
            - All required axes instances will be created automatically
            - It is recommended to use it when the plotting function
            creates multiple axes itself. It helps avoid warnings like
            'UserWarning: To output multiple subplots,
            the figure containing the passed axes is being cleared'
    **kwargs
        Keyword arguments passed to the plotting function.

    Returns
    -------
    Plot object returned by the last plotting.
    r   Nr   )matplotlib.pyplotr   _gen_default_plot_gen_two_subplotsgetgcfr   r   )fdefault_axeskwargsplt	gen_plotsretfigs          r   _check_plot_worksr     sb    6 $%	%	
C
**Xswwy
)CGGIC*6* 0*3/0 Jr   c              +      K    | di | yw)z'
    Create plot in a default way.
    Nr   r   r   r   r   s      r   r   r   0  s      +f+s   c              +     K   d|vr|j                  d        | di | | t        j                  j                  u rd|vsJ |j                  d      |d<    | di | yw)z9
    Create plot on two subplots forcefully created.
    r         Nr   )add_subplotpdplottingbootstrap_plotr   s      r   r   r   7  sf      6
+f+BKK&&&6!!!s+t
+f+s   A!A#)NT)T)r   Axes | Sequence[Axes]r=   boolreturnNone)NNN)NNNN)linearr  )r   r  r
  zSequence[Axes])r   r   r@   )r[   )r
  r  )F))__doc__
__future__r   typingr   numpyrU   pandas.core.dtypes.apir   pandasr  r   pandas._testing_testingr'   collections.abcr   r   r	   r   r   r1   r9   r>   rH   r^   r   rt   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r     s    #    /   ($+0'./&.((".=&@(,G^-$)X ": ?C:%z,^;  <,)Xr   