U
    Evf                     @   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   sP   |dkrt dt| ttj| td}|| 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 `~numpy.ndarray` of float
        NzMissing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric dataZdtype)Zotypes)
ValueErrorr   _validate_unitnp
atleast_1darrayobjectupdateZ	vectorize_mapping__getitem__float)valueunitaxisvalues r   7/tmp/pip-unpacked-wheel-7vhvci0g/matplotlib/category.py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   axisinfo<   s    


zStrCategoryConverter.axisinfoc                 C   s,   |j dk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_unitsW   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	   n   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__x   s    zStrCategoryLocator.__init__c                 C   s   t | j S N)listr(   r   r*   r   r   r   __call__   s    zStrCategoryLocator.__call__c                 C   s   |  S r,   r   )r*   ZvminZvmaxr   r   r   tick_values   s    zStrCategoryLocator.tick_valuesN)r"   r#   r$   __doc__r+   r/   r0   r   r   r   r   r   v   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 r&   r'   r)   r   r   r   r+      s    zStrCategoryFormatter.__init__Nc                 C   s   |  |gd S )Nr   )format_ticks)r*   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).0kvr.   r   r   
<dictcomp>   s      z5StrCategoryFormatter.format_ticks.<locals>.<dictcomp>c                    s   g | ]}  t|d qS ) )getround)r6   val)	r_mappingr   r   
<listcomp>   s     z5StrCategoryFormatter.format_ticks.<locals>.<listcomp>)r(   items)r*   r   r   )r>   r*   r   r2      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)
isinstancebytesdecodestrr   r   r   r   r5      s
    

zStrCategoryFormatter._text)N)	r"   r#   r$   r1   r+   r/   r2   r%   r5   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k	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   )r*   r   r   r   r   r+      s    	
zUnitData.__init__c                 C   sT   zt |  W nB tk
rN   ztj|  W n ttfk
rH   Y Y dS X Y nX dS )zY
        Helper method to check whether a string can be parsed as float or date.
        FT)r   r   dateutilparserparse	TypeError)r=   r   r   r   _str_is_convertible   s    zUnitData._str_is_convertiblec                 C   sz   t t j|td}d}t|D ]>}tjtt	f|d |rF| 
|}|| jkr"t| j| j|< q"|jrv|rvt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   TrF   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   Zcheck_isinstancerE   rC   rN   r   nextrI   size_loginfo)r*   r   Zconvertibler=   r   r   r   r      s    


zUnitData.update)N)r"   r#   r$   r+   r%   rN   r   r   r   r   r   r      s   

r   )r1   collectionsr   Zdateutil.parserrJ   rG   loggingZnumpyr
   Z
matplotlibr   r   r   	getLoggerr"   rR   ZConversionInterfacer   Locatorr   	Formatterr   r   registryrE   Zstr_rC   Zbytes_r   r   r   r   <module>   s   
\>