
    G8c                        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j                  e      Z G d dej                         Z G d dej$                        Z G d	 d
ej(                        Z G d d      Z e       ej.                  e<    e       ej.                  ej2                  <    e       ej.                  e<    e       ej.                  ej6                  <   y)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                   L    e Zd Zed        Zed        Zed        Zed        Zy)StrCategoryConverterc                 &   |t        d      t        j                  |       t        j                  t        j
                  | t                    }t        j                         5  t        d |D              }ddd       |j                  r4r2t        j                  dd       t        j                  |t              S |j                  |        t        j                  |j                   j"                  t        g      |      S # 1 sw Y   xY w)	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              3      K   | ]<  }t         j                  j                  |      xr t        |t        t
        f        > y wN)r   ConversionInterface
is_numlike
isinstancestrbytes).0vs     5/usr/lib/python3/dist-packages/matplotlib/category.py	<genexpr>z/StrCategoryConverter.convert.<locals>.<genexpr>:   sF      .!" #66AA!D A%/C<%@!@A .s   AAz3.5zSupport for passing numbers through unit converters is deprecated since %(since)s and support will be removed %(removal)s; use Axis.convert_units instead.)message)otypes)
ValueErrorr   _validate_unitnp
atleast_1darrayobjectr   'suppress_matplotlib_deprecation_warningallsizewarn_deprecatedasarrayfloatupdate	vectorize_mapping__getitem__)valueunitaxisvaluesr   s        r   convertzStrCategoryConverter.convert   s    * <    	++D1rxxV<=99; 	. .&,. .J	. ;;:    GH ::fE22FFr||DMM55ugFvNN	. 	.s   %DDc                     t         j                  |        t        | j                        }t	        | j                        }t        j                  ||      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   AxisInfo)r*   r+   r/   r0   s       r   axisinfozStrCategoryConverter.axisinfoG   sA    ( 	++D1 $DMM2%dmm4~~VF;;    c                     |j                   &|j                  t        |              |j                   S |j                   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
        )r   	set_unitsUnitDatar%   )datar+   s     r   default_unitsz"StrCategoryConverter.default_unitsb   sH    " ::NN8D>* zz JJd#zzr5   c                 :    t        | d      st        d|  d      y )Nr'   zProvided unit "zU" is not valid for a categorical converter, as it does not have a _mapping attribute.)hasattrr   )r*   s    r   r   z#StrCategoryConverter._validate_unity   s3    tZ(!$ (G GH H )r5   N)__name__
__module____qualname__staticmethodr-   r4   r:   r    r5   r   r   r      sU    )O )OV < <4  , H Hr5   r   c                   "    e Zd ZdZd Zd Zd Zy)r1   z1Tick at every integer mapping of the string data.c                     || _         yz
        Parameters
        ----------
        units_mapping : dict
            Mapping of category names (str) to indices (int).
        N_unitsselfunits_mappings     r   __init__zStrCategoryLocator.__init__        $r5   c                 H    t        | j                  j                               S r   )listrF   r,   )rH   s    r   __call__zStrCategoryLocator.__call__   s    DKK&&())r5   c                      |        S r   rA   )rH   vminvmaxs      r   tick_valueszStrCategoryLocator.tick_values   s	    vr5   N)r=   r>   r?   __doc__rJ   rN   rR   rA   r5   r   r1   r1      s    ;$*r5   r1   c                   4    e Zd ZdZd ZddZd Zed        Zy)r2   z0String representation of the data at every tick.c                     || _         yrD   rE   rG   s     r   rJ   zStrCategoryFormatter.__init__   rK   r5   Nc                 ,    | j                  |g      d   S )Nr   )format_ticks)rH   xposs      r   rN   zStrCategoryFormatter.__call__   s      !%a((r5   c                     | j                   j                         D ci c]  \  }}|| j                  |       }}}|D cg c]  }|j                  t	        |      d       c}S c c}}w c c}w )N )rF   items_textgetround)rH   r,   kr   	r_mappingvals         r   rW   z!StrCategoryFormatter.format_ticks   s\    26++2C2C2EF$!QQ

1%F	F9?@#	eCj"-@@ G@s   A&"A,c                     t        | t              r| j                  d      } | S t        | t              st        |       } | S )z0Convert text values into utf-8 or ascii strings.zutf-8)encoding)r   r   decoder   r)   s    r   r]   zStrCategoryFormatter._text   s?     eU#LL'L2E  E3'JEr5   r   )	r=   r>   r?   rS   rJ   rN   rW   r@   r]   rA   r5   r   r2   r2      s)    :$)A
  r5   r2   c                   *    e Zd ZddZed        Zd Zy)r8   Nc                 |    t               | _        t        j                         | _        || j                  |       yy)z
        Create mapping between unique categorical values and integer ids.

        Parameters
        ----------
        data : iterable
            sequence of string values
        N)r   r'   	itertoolscount_counterr%   )rH   r9   s     r   rJ   zUnitData.__init__   s2     $!)KK r5   c                     	 t        |        y# t        $ r9 	 t        j                  j	                  |        Y y# t        t
        f$ r Y Y yw xY ww xY w)zY
        Helper method to check whether a string can be parsed as float or date.
        FT)r$   r   dateutilparserparse	TypeError)rb   s    r   _str_is_convertiblezUnitData._str_is_convertible   sY    
	#J   	%%c*  	* 	s$    	A9AAAAc                    t        j                  t        j                  |t                    }d}t	        j
                  |      D ]g  }t        j                  t        t        f|       |r| j                  |      }|| j                  vsFt        | j                        | j                  |<   i |j                  r|rt        j!                  d       yyy)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   check_isinstancer   r   rq   r'   nextrk   r!   _loginfo)rH   r9   convertiblerb   s       r   r%   zUnitData.update   s     }}RXXd&9:''- 	9C!!3,c:"66s;$--'%)$--%8c"	9 99II ? @ %9r5   r   )r=   r>   r?   rJ   r@   rq   r%   rA   r5   r   r8   r8      s!      @r5   r8   )rS   collectionsr   dateutil.parserrm   ri   loggingnumpyr   
matplotlibr   r   r   	getLoggerr=   rv   r   r   Locatorr1   	Formatterr2   r8   registryr   str_r   bytes_rA   r5   r   <module>r      s   
 $     * * w"dH544 dHN (6++ <:@ :@| +,s .0rww ,.u 02ryy r5   