3
$ßL]E  ã               @   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	j
Z
ddljZddljZej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eƒZeƒ eje< eƒ ejej< eƒ eje< eƒ ejej< dS )aÈ  
Module that allows plotting of string "category" data.  i.e.
``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, provides a tick locator and formatter, and the
class:`.UnitData` that creates and stores the string-to-integer mapping.
é    )ÚOrderedDictNc               @   s0   e Zd Zedd„ ƒZedd„ ƒZedd„ ƒZdS )ÚStrCategoryConverterc             C   sn   |dkrt dƒ‚tjtj| td�ƒ}tdd„ |D ƒƒrDtj|td�S |j|ƒ tj	|j
jtgd�}||ƒ}|S )a  Convert strings in value to floats using
        mapping information store in the unit object.

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

            .. note:: *axis* is unused.

        Returns
        -------
        mapped_value : 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j|ƒo t|ttfƒ V  qd S )N)ÚunitsÚConversionInterfaceZ
is_numlikeÚ
isinstanceÚstrÚbytes)Ú.0Úv© r   ú;/tmp/pip-install-edjban26/matplotlib/matplotlib/category.pyú	<genexpr><   s   z/StrCategoryConverter.convert.<locals>.<genexpr>)Zotypes)Ú
ValueErrorÚnpÚ
atleast_1dÚarrayÚobjectÚallZasarrayÚfloatÚupdateZ	vectorizeÚ_mappingÚ__getitem__)ÚvalueÚunitÚaxisÚvaluesZstr2idxZmapped_valuer   r   r   Úconvert   s    



zStrCategoryConverter.convertc             C   s"   t | jƒ}t| jƒ}tj||d�S )a®  Sets 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
        -------
        axisinfo : `~matplotlib.units.AxisInfo`
            Information to support default tick labeling

        .. note: axis is not used
        )ÚmajlocÚmajfmt)ÚStrCategoryLocatorr   ÚStrCategoryFormatterr   ZAxisInfo)r   r   r   r   r   r   r   ÚaxisinfoI   s    

zStrCategoryConverter.axisinfoc             C   s,   |j dkr|jt| ƒƒ n|j j| ƒ |j S )a^  Sets and updates the :class:`~matplotlib.Axis.axis` units.

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

        Returns
        -------
        class : `.UnitData`
            object storing string to integer mapping
        N)r   Z	set_unitsÚUnitDatar   )Údatar   r   r   r   Údefault_unitsa   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    z0tick at every integer mapping of the string datac             C   s
   || _ dS )z{
        Parameters
        -----------
        units_mapping : Dict[str, int]
             string:integer mapping
        N)Ú_units)ÚselfÚunits_mappingr   r   r   Ú__init__{   s    zStrCategoryLocator.__init__c             C   s   t | jjƒ ƒS )N)Úlistr*   r   )r+   r   r   r   Ú__call__„   s    zStrCategoryLocator.__call__c             C   s   | ƒ S )Nr   )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    y   s   	r    c               @   s.   e Zd ZdZdd„ Zd	dd„Zedd„ ƒZdS )
r!   z/String representation of the data at every tickc             C   s
   || _ dS )zy
        Parameters
        ----------
        units_mapping : Dict[Str, int]
            string:integer mapping
        N)r*   )r+   r,   r   r   r   r-   �   s    zStrCategoryFormatter.__init__Nc             C   s6   |d krdS dd„ | j jƒ D ƒ}|jttj|ƒƒdƒS )NÚ c             S   s   i | ]\}}t j|ƒ|“qS r   )r!   Ú_text)r
   Úkr   r   r   r   ú
<dictcomp>™   s   z1StrCategoryFormatter.__call__.<locals>.<dictcomp>)r*   ÚitemsÚgetÚintr   Úround)r+   ÚxÚposZ	r_mappingr   r   r   r/   –   s
    zStrCategoryFormatter.__call__c             C   s.   t | tƒr| jdd�} nt | tƒs*t| ƒ} | S )z:Converts text values into utf-8 or ascii strings.
        zutf-8)Úencoding)r   r	   Údecoder   )r   r   r   r   r3   �   s
    

zStrCategoryFormatter._text)N)r&   r'   r(   r1   r-   r/   r)   r3   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jƒ | _|dk	r$| j|ƒ 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   sL   yt | ƒ W n: tk
rF   ytjj| ƒ W n tk
r@   dS X Y nX dS )zb
        Helper method to see if a string can be cast to float or
        parsed as date.
        FT)r   r   ÚdateutilÚparserÚparse)Úvalr   r   r   Ú_str_is_convertible·   s    zUnitData._str_is_convertiblec             C   s„   t jt j|td�ƒ}d}xXtj|ƒD ]J}t|ttfƒsFt	dj
|d�ƒ‚|rT| j|ƒ}|| jkr$t| jƒ| j|< q$W |r€tjdƒ dS )a  Maps new values to integer identifiers.

        Parameters
        ----------
        data : iterable
            sequence of string values

        Raises
        ------
        TypeError
              If the value in data is not a string, unicode, bytes type
        )r   Tz{val!r} is not a string)rD   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   r   r	   Ú	TypeErrorÚformatrE   r   Únextr@   Ú_logÚinfo)r+   r$   ZconvertiblerD   r   r   r   r   Æ   s    

zUnitData.update)N)r&   r'   r(   r-   r)   rE   r   r   r   r   r   r#   ¨   s   
r#   )r1   Úcollectionsr   Zdateutil.parserrA   r>   ÚloggingZnumpyr   Zmatplotlib.cbookZcbookZmatplotlib.unitsr   Zmatplotlib.tickerZtickerÚ	getLoggerr&   rJ   r   r   ÚLocatorr    Ú	Formatterr!   r   r#   Úregistryr   Zstr_r	   Zbytes_r   r   r   r   Ú<module>   s"   



\@