o
    i                     @   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)cbooktickerunitsc                   @   s0   e Zd Zedd Zedd Zedd ZdS )StrCategoryConverterc                 C   sf   |du rt dttj| td}tdd |D r"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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   N/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/matplotlib/category.py	<genexpr>8   s    
z/StrCategoryConverter.convert.<locals>.<genexpr>)otypes)
ValueErrornp
atleast_1darrayobjectallasarrayfloatupdate	vectorize_mapping__getitem__)valueunitaxisvaluesr   r   r   convert   s   
zStrCategoryConverter.convertc                 C   s"   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

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

        .. note: axis is not used
        )majlocmajfmt)StrCategoryLocatorr   StrCategoryFormatterr   AxisInfo)r"   r#   r&   r'   r   r   r   axisinfo@   s   

zStrCategoryConverter.axisinfoc                 C   s0   |j du r|t|  |j S |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   	set_unitsUnitDatar   )datar#   r   r   r   default_unitsY   s
   
z"StrCategoryConverter.default_unitsN)__name__
__module____qualname__staticmethodr%   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   
   || _ dS )z
        Parameters
        -----------
        units_mapping : dict
            Mapping of category names (str) to indices (int).
        N_unitsselfunits_mappingr   r   r   __init__s      
zStrCategoryLocator.__init__c                 C   s   t | j S r	   )listr6   r$   r8   r   r   r   __call__|   s   zStrCategoryLocator.__call__c                 C   s   |  S r	   r   )r8   vminvmaxr   r   r   tick_values   s   zStrCategoryLocator.tick_valuesN)r0   r1   r2   __doc__r:   r>   rA   r   r   r   r   r(   q   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   r4   )z
        Parameters
        ----------
        units_mapping : dict
            Mapping of category names (str) to indices (int).
        Nr5   r7   r   r   r   r:      r;   zStrCategoryFormatter.__init__Nc                 C   s   |  |gd S )Nr   )format_ticks)r8   xposr   r   r   r>      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   r=   r   r   
<dictcomp>       z5StrCategoryFormatter.format_ticks.<locals>.<dictcomp>c                    s   g | ]
}  t|d qS ) )getround)r   val)	r_mappingr   r   
<listcomp>   rI   z5StrCategoryFormatter.format_ticks.<locals>.<listcomp>)r6   items)r8   r$   r   )rN   r8   r   rC      s   z!StrCategoryFormatter.format_ticksc                 C   s0   t | tr| jdd} | S 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   rF      s   

zStrCategoryFormatter._textr	   )	r0   r1   r2   rB   r:   r>   rC   r3   rF   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 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   )r8   r.   r   r   r   r:      s
   	
zUnitData.__init__c                 C   sN   zt |  W dS  ty&   z
tj|  W Y dS  ttfy%   Y Y dS w w )zY
        Helper method to check whether a string can be parsed as float or date.
        FT)r   r   dateutilparserparse	TypeError)rM   r   r   r   _str_is_convertible   s   
zUnitData._str_is_convertiblec                 C   sx   t t j|td}d}t|D ]}tjtt	f|d |r#| 
|}|| jvr0t| j| j|< q|r:td dS 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   TrS   zUsing 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   _check_isinstancer   r   r[   r   nextrV   _loginfo)r8   r.   convertiblerM   r   r   r   r      s   

zUnitData.updater	   )r0   r1   r2   r:   r3   r[   r   r   r   r   r   r-      s
    

r-   )rB   collectionsr   dateutil.parserrW   rT   loggingnumpyr   
matplotlibr   r   r   	getLoggerr0   r_   r
   r   Locatorr(   	Formatterr)   r-   registryr   str_r   bytes_r   r   r   r   <module>   s     
W>