
    g                        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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       xej0                  e<   xej0                  ej4                  <   xej0                  e<   ej0                  ej8                  <   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cbooktickerunitsc                   L    e Zd Zed        Zed        Zed        Zed        Zy)StrCategoryConverterc                 h   |t        d      t        j                  |       t        j                  t        j
                  | t                    }|j                  |        t        j                  |j                  j                  t        g      |      }t        j                  |       s|S |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
        zMissing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric datadtype)otypesr   )
ValueErrorr	   _validate_unitnp
atleast_1darrayobjectupdate	vectorize_mapping__getitem__floatr   is_scalar_or_string)valueunitaxisvaluesss        P/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/matplotlib/category.pyconvertzStrCategoryConverter.convert   s    * <    	++D1rxxV<=FCBLL22E7CFK11%8qBadB    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#   r$   s       r   axisinfozStrCategoryConverter.axisinfo=   sA    ( 	++D1 $DMM2%dmm4~~VF;;r!   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_unitsX   sH    " ::NN8D>* zz JJd#zzr!   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_unito   s3    tZ(!$ (G GH H )r!   N)__name__
__module____qualname__staticmethodr    r(   r-   r    r!   r   r	   r	      sU    C CB < <4  , H Hr!   r	   c                   "    e Zd ZdZd Zd Zd Zy)r%   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__y        $r!   c                 H    t        | j                  j                               S N)listr9   r   )r;   s    r   __call__zStrCategoryLocator.__call__   s    DKK&&())r!   c                      |        S r@   r4   )r;   vminvmaxs      r   tick_valueszStrCategoryLocator.tick_values   s	    vr!   N)r0   r1   r2   __doc__r=   rB   rF   r4   r!   r   r%   r%   w   s    ;$*r!   r%   c                   4    e Zd ZdZd ZddZd Zed        Zy)r&   z0String representation of the data at every tick.c                     || _         yr7   r8   r:   s     r   r=   zStrCategoryFormatter.__init__   r>   r!   Nc                 ,    | j                  |g      d   S )Nr   )format_ticks)r;   xposs      r   rB   zStrCategoryFormatter.__call__   s      !%a((r!   c                     | j                   j                         D ci c]  \  }}|| j                  |       }}}|D cg c]  }|j                  t	        |      d       c}S c c}}w c c}w )N )r9   items_textgetround)r;   r   kv	r_mappingvals         r   rK   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)
isinstancebytesdecodestrr   s    r   rQ   zStrCategoryFormatter._text   s?     eU#LL'L2E  E3'JEr!   r@   )	r0   r1   r2   rG   r=   rB   rK   r3   rQ   r4   r!   r   r&   r&      s)    :$)A
  r!   r&   c                   *    e Zd ZddZed        Zd Zy)r+   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   )r;   r,   s     r   r=   zUnitData.__init__   s2     $!)KK r!   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)rW   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   Tr^   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[   ri   r   nextrc   size_loginfo)r;   r,   convertiblerW   s       r   r   zUnitData.update   s     }}RXXd&9:''- 	9C!!3,c:"66s;$--'%)$--%8c"	9 99II ? @ %9r!   r@   )r0   r1   r2   r=   r3   ri   r   r4   r!   r   r+   r+      s!      @r!   r+   )rG   collectionsr   dateutil.parserre   ra   loggingnumpyr   
matplotlibr   r   r   r   	getLoggerr0   ro   ConversionInterfacer	   Locatorr%   	Formatterr&   r+   registryr]   str_r[   bytes_r4   r!   r   <module>r~      s   
 $     1 1 w"ZH544 ZHz (6++ <:@ :@D !5 67s  7	NN2777	NN5	NN299r!   