a
    ëÝEbñ  ã                   @   sÊ   d Z ddlmZ ddlZddlZddlZddlZddl	m
Z
mZmZ e e¡ZG dd„ dejƒZG dd„ dejƒZG d	d
„ d
ejƒZG dd„ dƒZeƒ eje< eƒ ejej< eƒ eje< eƒ ejej< dS )a·  
Plotting of string "category" data: ``plot(['d', 'f', 'a'], [1, 2, 3])`` will
plot three points with x-axis values of 'd', 'f', 'a'.

See :doc:`/gallery/lines_bars_and_markers/categorical_variables` for an
example.

The module uses Matplotlib's `matplotlib.units` mechanism to convert from
strings to integers and provides a tick locator, a tick formatter, and the
`.UnitData` class that creates and stores the string-to-integer mapping.
é    )ÚOrderedDictN)Ú_apiÚtickerÚunitsc                   @   s<   e Zd Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZd	S )
ÚStrCategoryConverterc                 C   sª   |du rt dƒ‚t |¡ t tj| td�¡}t ¡ �" t	dd„ |D ƒƒ}W d  ƒ n1 s^0    Y  |rˆtj
ddd� tj|td�S | |¡ tj|jjtgd	�|ƒS )
a  
        Convert strings in *value* to floats using mapping information stored
        in the *unit* object.

        Parameters
        ----------
        value : str or iterable
            Value or list of values to be converted.
        unit : `.UnitData`
            An object mapping strings to integers.
        axis : `~matplotlib.axis.Axis`
            The axis on which the converted value is plotted.

            .. note:: *axis* is unused.

        Returns
        -------
        float or ndarray[float]
        NzMissing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data©Zdtypec                 s   s*   | ]"}t j |¡o t|ttfƒ V  qd S ©N)r   ÚConversionInterfaceÚ
is_numlikeÚ
isinstanceÚstrÚbytes)Ú.0Úv© r   úS/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/matplotlib/category.pyÚ	<genexpr>:   s   þz/StrCategoryConverter.convert.<locals>.<genexpr>z3.5zšSupport for passing numbers through unit converters is deprecated since %(since)s and support will be removed %(removal)s; use Axis.convert_units instead.)Úmessage)Zotypes)Ú
ValueErrorr   Ú_validate_unitÚnpÚ
atleast_1dÚarrayÚobjectr   Z'suppress_matplotlib_deprecation_warningÚallZwarn_deprecatedZasarrayÚfloatÚupdateZ	vectorizeÚ_mappingÚ__getitem__)ÚvalueÚunitÚaxisÚvaluesr
   r   r   r   Úconvert   s"    ÿ

þ&ÿ
zStrCategoryConverter.convertc                 C   s,   t  | ¡ t| jƒ}t| jƒ}tj||d�S )a´  
        Set the default axis ticks and labels.

        Parameters
        ----------
        unit : `.UnitData`
            object string unit information for value
        axis : `~matplotlib.axis.Axis`
            axis for which information is being set

            .. note:: *axis* is not used

        Returns
        -------
        `~matplotlib.units.AxisInfo`
            Information to support default tick labeling

        )ÚmajlocÚmajfmt)r   r   ÚStrCategoryLocatorr   ÚStrCategoryFormatterr   ZAxisInfo)r    r!   r$   r%   r   r   r   ÚaxisinfoG   s    


zStrCategoryConverter.axisinfoc                 C   s,   |j du r| t| ƒ¡ n|j  | ¡ |j S )aO  
        Set and update the `~matplotlib.axis.Axis` units.

        Parameters
        ----------
        data : str or iterable of str
        axis : `~matplotlib.axis.Axis`
            axis on which the data is plotted

        Returns
        -------
        `.UnitData`
            object storing string to integer mapping
        N)r   Z	set_unitsÚUnitDatar   )Údatar!   r   r   r   Údefault_unitsb   s    
z"StrCategoryConverter.default_unitsc                 C   s   t | dƒstd| › d�ƒ‚d S )Nr   zProvided unit "zU" is not valid for a categorical converter, as it does not have a _mapping attribute.)Úhasattrr   )r    r   r   r   r   y   s    

ÿz#StrCategoryConverter._validate_unitN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr#   r(   r+   r   r   r   r   r   r      s   
+

r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r&   z1Tick at every integer mapping of the string data.c                 C   s
   || _ dS ©zŠ
        Parameters
        ----------
        units_mapping : dict
            Mapping of category names (str) to indices (int).
        N©Ú_units©ÚselfZunits_mappingr   r   r   Ú__init__ƒ   s    zStrCategoryLocator.__init__c                 C   s   t | j ¡ ƒS r   )Úlistr3   r"   ©r5   r   r   r   Ú__call__Œ   s    zStrCategoryLocator.__call__c                 C   s   | ƒ S r   r   )r5   ZvminZvmaxr   r   r   Útick_values�   s    zStrCategoryLocator.tick_valuesN)r-   r.   r/   Ú__doc__r6   r9   r:   r   r   r   r   r&   �   s   	r&   c                   @   s6   e Zd ZdZdd„ Zddd„Zdd„ Zed	d
„ ƒZdS )r'   z0String representation of the data at every tick.c                 C   s
   || _ dS r1   r2   r4   r   r   r   r6   —   s    zStrCategoryFormatter.__init__Nc                 C   s   |   |g¡d S )Nr   )Úformat_ticks)r5   ÚxÚposr   r   r   r9       s    zStrCategoryFormatter.__call__c                    s*   ‡fdd„ˆj  ¡ D ƒ‰ ‡ fdd„|D ƒS )Nc                    s   i | ]\}}|ˆ   |¡“qS r   )Ú_text)r   Úkr   r8   r   r   Ú
<dictcomp>¦   ó    z5StrCategoryFormatter.format_ticks.<locals>.<dictcomp>c                    s   g | ]}ˆ   t|ƒd ¡‘qS )Ú )ÚgetÚround)r   Úval)Ú	r_mappingr   r   Ú
<listcomp>§   rB   z5StrCategoryFormatter.format_ticks.<locals>.<listcomp>)r3   Úitems)r5   r"   r   )rG   r5   r   r<   ¤   s    z!StrCategoryFormatter.format_ticksc                 C   s.   t | tƒr| jdd�} nt | tƒs*t| ƒ} | S )z0Convert text values into utf-8 or ascii strings.zutf-8)Úencoding)r   r   Údecoder   ©r   r   r   r   r?   ©   s
    

zStrCategoryFormatter._text)N)	r-   r.   r/   r;   r6   r9   r<   r0   r?   r   r   r   r   r'   •   s   	
r'   c                   @   s*   e Zd Zddd„Zedd„ ƒZdd„ ZdS )	r)   Nc                 C   s(   t ƒ | _t ¡ | _|dur$|  |¡ dS )z¸
        Create mapping between unique categorical values and integer ids.

        Parameters
        ----------
        data : iterable
            sequence of string values
        N)r   r   Ú	itertoolsÚcountÚ_counterr   )r5   r*   r   r   r   r6   ´   s    	
zUnitData.__init__c                 C   sP   zt | ƒ W n> tyJ   ztj | ¡ W n ttfyD   Y Y dS 0 Y n0 dS )zY
        Helper method to check whether a string can be parsed as float or date.
        FT)r   r   ÚdateutilÚparserÚparseÚ	TypeError)rF   r   r   r   Ú_str_is_convertibleÂ   s    zUnitData._str_is_convertiblec                 C   st   t  t j|td�¡}d}t |¡D ]>}tjtt	f|d� |rF|  
|¡}|| jvr"t| jƒ| j|< q"|rpt d¡ dS )zõ
        Map new values to integer identifiers.

        Parameters
        ----------
        data : iterable of str or bytes

        Raises
        ------
        TypeError
            If elements in *data* are neither str nor bytes.
        r   TrL   z½Using categorical units to plot a list of strings that are all parsable as floats or dates. If these strings should be plotted as numbers, cast to the appropriate data type before plotting.N)r   r   r   r   r   Úfromkeysr   Zcheck_isinstancer   r   rT   r   ÚnextrO   Ú_logÚinfo)r5   r*   ZconvertiblerF   r   r   r   r   Ñ   s    

zUnitData.update)N)r-   r.   r/   r6   r0   rT   r   r   r   r   r   r)   ³   s   

r)   )r;   Úcollectionsr   Zdateutil.parserrP   rM   ÚloggingÚnumpyr   Z
matplotlibr   r   r   Ú	getLoggerr-   rW   r	   r   ÚLocatorr&   Ú	Formatterr'   r)   Úregistryr   Zstr_r   Úbytes_r   r   r   r   Ú<module>   s   
g>