3
ƽhgA                 @   s   d Z ddlmZ ddlZddlZddlmZ ddlZddl	j
Z
ddljZddlmZ ddlmZ dd Zejd	d
ddd Zejd Zdd ZG dd deZe Ze je eeZdddZdddZG dd dZdS )a  
Builtin colormaps, colormap handling utilities, and the `ScalarMappable` mixin.

.. seealso::

  :doc:`/gallery/color/colormap_reference` for a list of builtin colormaps.

  :doc:`/tutorials/colors/colormap-manipulation` for examples of how to
  make colormaps.

  :doc:`/tutorials/colors/colormaps` an in-depth discussion of
  choosing colormaps.

  :doc:`/tutorials/colors/colormapnorms` for more details about data
  normalization.
    )MutableMappingN)ma)datad)cmapsc             C   s   | d| S )N    )fxr   r   3/tmp/pip-build-7iwl8md4/matplotlib/matplotlib/cm.py	_reverser   s    r   z3.2zColormap.reversed())alternativec             C   sN   i }xD| j  D ]8\}}t|r,tjt|}ndd t|D }|||< qW |S )z:Can only handle specification *data* in dictionary format.c             S   s    g | ]\}}}d | ||fqS )g      ?r   ).0r	   Zy0y1r   r   r
   
<listcomp>-   s    zrevcmap.<locals>.<listcomp>)itemscallable	functoolspartialr   reversed)dataZdata_rkeyvalZvalnewr   r   r
   revcmap#   s    r   z	image.lutc              C   s   t } xVtj D ]J\}}d|kr.tj||tn&d|krFtj|d |ntjj||t| |< qW x2t| j	 D ]"}|j
 }d|_d|_|| |j< qlW | S )zw
    Generate a dict mapping standard colormap names to standard colormaps, as
    well as the reversed colormaps.
    ZredZlistedT)cmaps_listedr   r   colorsLinearSegmentedColormapLUTSIZEZListedColormap	from_listlistvaluesr   _globalname)cmap_dr!   speccmapZrmapr   r   r
   _gen_cmap_registry5   s    r%   c               @   sR   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dddZ
dd ZdS )_DeprecatedCmapDictWrapperz1Dictionary mapping for deprecated _cmap_d access.c             C   s
   || _ d S )N)_cmap_registry)selfZcmap_registryr   r   r
   __init__N   s    z#_DeprecatedCmapDictWrapper.__init__c             C   s   | j   | jj| d S )N)_warn_deprecatedr'   __delitem__)r(   r   r   r   r
   r+   Q   s    z&_DeprecatedCmapDictWrapper.__delitem__c             C   s   | j   | jj|S )N)r*   r'   __getitem__)r(   r   r   r   r
   r,   U   s    z&_DeprecatedCmapDictWrapper.__getitem__c             C   s   | j   | jj S )N)r*   r'   __iter__)r(   r   r   r
   r-   Y   s    z#_DeprecatedCmapDictWrapper.__iter__c             C   s   | j   | jj S )N)r*   r'   __len__)r(   r   r   r
   r.   ]   s    z"_DeprecatedCmapDictWrapper.__len__c             C   s   | j   | jj|| d S )N)r*   r'   __setitem__)r(   r   r   r   r   r
   r/   a   s    z&_DeprecatedCmapDictWrapper.__setitem__Nc             C   s   | j   | jj||S )N)r*   r'   get)r(   r   defaultr   r   r
   r0   e   s    z_DeprecatedCmapDictWrapper.getc             C   s   t jdddd d S )Nz3.3zCThe global colormaps dictionary is no longer considered public API.zSPlease use register_cmap() and get_cmap() to access the contents of the dictionary.)messager   )cbookwarn_deprecated)r(   r   r   r
   r*   i   s    z+_DeprecatedCmapDictWrapper._warn_deprecated)N)__name__
__module____qualname____doc__r)   r+   r,   r-   r.   r/   r0   r*   r   r   r   r
   r&   K   s   
r&   c             C   s   t jtdf| d | dkrRy
|j} W n, tk
rP } ztd|W Y dd}~X nX t|tjrpd|_	|t
| < dS |dk	s|dk	rt jddd |dkrtjd }tj| ||}d|_	|t
| < dS )	aC  
    Add a colormap to the set recognized by :func:`get_cmap`.

    It can be used in two ways::

        register_cmap(name='swirly', cmap=swirly_cmap)

        register_cmap(name='choppy', data=choppydata, lut=128)

    In the first case, *cmap* must be a :class:`matplotlib.colors.Colormap`
    instance.  The *name* is optional; if absent, the name will
    be the :attr:`~matplotlib.colors.Colormap.name` attribute of the *cmap*.

    The second case is deprecated. Here, the three arguments are passed to
    the :class:`~matplotlib.colors.LinearSegmentedColormap` initializer,
    and the resulting colormap is registered. Instead of this implicit
    colormap creation, create a `.LinearSegmentedColormap` and use the first
    case: ``register_cmap(cmap=LinearSegmentedColormap(name, data, lut))``.

    Notes
    -----
    Registering a colormap stores a reference to the colormap object
    which can currently be modified and inadvertantly change the global
    colormap state. This behavior is deprecated and in Matplotlib 3.5
    the registered colormap will be immutable.
    N)r!   z+Arguments must include a name or a ColormapTz3.3zPassing raw data via parameters data and lut to register_cmap() is deprecated since %(since)s and will become an error %(removal)s. Instead use: register_cmap(cmap=LinearSegmentedColormap(name, data, lut)))r2   z	image.lut)r3   _check_isinstancestrr!   AttributeError
ValueError
isinstancer   Colormapr    r'   r4   mplrcParamsr   )r!   r$   r   luterrr   r   r
   register_cmap|   s(    

rC   c             C   sV   | dkrt jd } t| tjr"| S tjtt| d |dkrDt|  S t|  j	|S dS )a_  
    Get a colormap instance, defaulting to rc values if *name* is None.

    Colormaps added with :func:`register_cmap` take precedence over
    built-in colormaps.

    Notes
    -----
    Currently, this returns the global colormap object, which is deprecated.
    In Matplotlib 3.5, you will no longer be able to modify the global
    colormaps in-place.

    Parameters
    ----------
    name : `matplotlib.colors.Colormap` or str or None, default: None
        If a `.Colormap` instance, it will be returned. Otherwise, the name of
        a colormap known to Matplotlib, which will be resampled by *lut*. The
        default, None, means :rc:`image.cmap`.
    lut : int or None, default: None
        If *name* is not already a Colormap instance and *lut* is not None, the
        colormap will be resampled to have *lut* entries in the lookup table.
    Nz
image.cmap)r!   )
r?   r@   r=   r   r>   r3   Z_check_in_listsortedr'   Z	_resample)r!   rA   r   r   r
   get_cmap   s    
rE   c               @   s   e Zd ZdZd*ddZdd Zd+d	d
Zdd Zdd Zdd Z	dd Z
d,ddZdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zejd%Zejd%d&d' Zejd%d(d) ZdS )-ScalarMappablez
    A mixin class to map scalar data to RGBA.

    The ScalarMappable applies data normalization before returning RGBA colors
    from the given colormap.
    Nc             C   sD   d| _ d| _| j| d| _| j| d| _tj | _ddi| _	dS )a  

        Parameters
        ----------
        norm : `matplotlib.colors.Normalize` (or subclass thereof)
            The normalizing object which scales data, typically into the
            interval ``[0, 1]``.
            If *None*, *norm* defaults to a *colors.Normalize* object which
            initializes its scaling based on the first data processed.
        cmap : str or `~matplotlib.colors.Colormap`
            The colormap used to map normalized data values to RGBA colors.
        NarrayF)
_Anormset_normr$   set_cmapZcolorbarr3   ZCallbackRegistrycallbacksSM_update_dict)r(   rI   r$   r   r   r
   r)      s    


zScalarMappable.__init__c             C   s>   |dk	s|dk	r2| j || |dk	r2tjddd | j  dS )a,  
        Helper for initial scaling.

        Used by public functions that create a ScalarMappable and support
        parameters *vmin*, *vmax* and *norm*. This makes sure that a *norm*
        will take precedence over *vmin*, *vmax*.

        Note that this method does not set the norm.
        Nz3.3zPassing parameters norm and vmin/vmax simultaneously is deprecated since %(since)s and will become an error %(removal)s. Please pass vmin/vmax directly to the norm when creating it.)r2   )set_climr3   r4   autoscale_None)r(   rI   vminvmaxr   r   r
   _scale_norm   s    
zScalarMappable._scale_normFTc       	      C   s  y<|j dkr<|jd dkr|dkr*d}|jtjkrDtj|d }|jdd \}}tj||df|jd}||ddddddf< ||dddddf< n|jd dkr|}ntd|jjd	kr|r|j dks|j	 d
k rtd|r8|d j
tj}n4|jtjkr*|s8|j
tjd }ntd|j |S W n tk
rT   Y nX tj|}|rp| j|}| j|||d}|S )a  
        Return a normalized rgba array corresponding to *x*.

        In the normal case, *x* is a 1-D or 2-D sequence of scalars, and
        the corresponding ndarray of rgba values will be returned,
        based on the norm and colormap set for this ScalarMappable.

        There is one special case, for handling images that are already
        rgb or rgba, such as might have been read from an image file.
        If *x* is an ndarray with 3 dimensions,
        and the last dimension is either 3 or 4, then it will be
        treated as an rgb or rgba array, and no mapping will be done.
        The array can be uint8, or it can be floating point with
        values in the 0-1 range; otherwise a ValueError will be raised.
        If it is a masked array, the mask will be ignored.
        If the last dimension is 3, the *alpha* kwarg (defaulting to 1)
        will be used to fill in the transparency.  If the last dimension
        is 4, the *alpha* kwarg is ignored; it does not
        replace the pre-existing alpha.  A ValueError will be raised
        if the third dimension is other than 3 or 4.

        In either case, if *bytes* is *False* (default), the rgba
        array will be floats in the 0-1 range; if it is *True*,
        the returned rgba array will be uint8 in the 0 to 255 range.

        If norm is False, no normalization of the input data is
        performed, and it is assumed to be in the range (0-1).

              Nr         )shapedtypezThird dimension must be 3 or 4r   r   z:Floating point image RGB values must be in the 0..1 range.z9Image RGB array must be uint8 or floating point; found %s)alphabytes)ndimrW   rX   npZuint8emptyr<   kindmaxminZastypeZfloat32r;   r   ZasarrayrI   r$   )	r(   r	   rY   rZ   rI   mnZxxZrgbar   r   r
   to_rgba
  s@    


zScalarMappable.to_rgbac             C   s   || _ d| jd< dS )zv
        Set the image array from numpy array *A*.

        Parameters
        ----------
        A : ndarray
        TrG   N)rH   rM   )r(   Ar   r   r
   	set_arrayP  s    zScalarMappable.set_arrayc             C   s   | j S )zReturn the data array.)rH   )r(   r   r   r
   	get_array[  s    zScalarMappable.get_arrayc             C   s   | j S )z Return the `.Colormap` instance.)r$   )r(   r   r   r
   rE   _  s    zScalarMappable.get_cmapc             C   s   | j j| j jfS )zV
        Return the values (min, max) that are mapped to the colormap limits.
        )rI   rP   rQ   )r(   r   r   r
   get_climc  s    zScalarMappable.get_climc             C   sf   |dkr.y|\}}W n t tfk
r,   Y nX |dk	rDtj|| j_|dk	rZtj|| j_| j  dS )a>  
        Set the norm limits for image scaling.

        Parameters
        ----------
        vmin, vmax : float
             The limits.

             The limits may also be passed as a tuple (*vmin*, *vmax*) as a
             single positional argument.

             .. ACCEPTS: (vmin: float, vmax: float)
        N)	TypeErrorr<   r   Z_sanitize_extremarI   rP   rQ   changed)r(   rP   rQ   r   r   r
   rN   i  s    zScalarMappable.set_climc             C   s   dS )zU
        Returns
        -------
        float
            Always returns 1.
        g      ?r   )r(   r   r   r
   	get_alpha  s    zScalarMappable.get_alphac             C   s(   | j dk}t|}|| _ |s$| j  dS )z
        Set the colormap for luminance data.

        Parameters
        ----------
        cmap : `.Colormap` or str or None
        N)r$   rE   ri   )r(   r$   in_initr   r   r
   rK     s
    
zScalarMappable.set_cmapc             C   sD   t jtjdf|d | jdk}|dkr.tj }|| _|s@| j  dS )aX  
        Set the normalization instance.

        Parameters
        ----------
        norm : `.Normalize` or None

        Notes
        -----
        If there are any colorbars using the mappable for this norm, setting
        the norm of the mappable will reset the norm, locator, and formatters
        on the colorbar to default.
        N)rI   )r3   r9   r   Z	NormalizerI   ri   )r(   rI   rk   r   r   r
   rJ     s    
zScalarMappable.set_normc             C   s,   | j dkrtd| jj| j  | j  dS )zb
        Autoscale the scalar limits on the norm instance using the
        current array
        Nz%You must first set_array for mappable)rH   rh   rI   	autoscaleri   )r(   r   r   r
   rl     s    
zScalarMappable.autoscalec             C   s,   | j dkrtd| jj| j  | j  dS )z
        Autoscale the scalar limits on the norm instance using the
        current array, changing only limits that are None
        Nz%You must first set_array for mappable)rH   rh   rI   rO   ri   )r(   r   r   r
   rO     s    
zScalarMappable.autoscale_Nonec             C   s   d| j |< dS )zz
        Add an entry to a dictionary of boolean flags
        that are set to True when the mappable is changed.
        FN)rM   )r(   checkerr   r   r
   _add_checker  s    zScalarMappable._add_checkerc             C   s   | j | rd| j |< dS dS )z9Return whether mappable has changed since the last check.FT)rM   )r(   rm   r   r   r
   _check_update  s    

zScalarMappable._check_updatec             C   s2   | j jd|  x| jD ]}d| j|< qW d| _dS )z
        Call this whenever the mappable is changed to notify all the
        callbackSM listeners to the 'changed' signal.
        ri   TN)rL   processrM   stale)r(   r   r   r   r
   ri     s    zScalarMappable.changedz3.3c             C   s
   | j |S )N)rn   )r(   rm   r   r   r
   add_checker  s    zScalarMappable.add_checkerc             C   s
   | j |S )N)ro   )r(   rm   r   r   r
   check_update  s    zScalarMappable.check_update)NN)NFT)NN)r5   r6   r7   r8   r)   rR   rc   re   rf   rE   rg   rN   rj   rK   rJ   rl   rO   rn   ro   ri   r3   Z_deprecate_privatize_attributeZupdate_dict
deprecatedrr   rs   r   r   r   r
   rF      s(   

F





rF   )NNNN)NN) r8   collections.abcr   r   numpyr\   r   Z
matplotlibr?   Zmatplotlib.colorsr   Zmatplotlib.cbookr3   Zmatplotlib._cmr   Zmatplotlib._cm_listedr   r   r   rt   r   r@   r   r%   r&   r'   localsupdater"   rC   rE   rF   r   r   r   r
   <module>   s(   


(
5
"