U
    CvfGx                  (   @  sx  d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZ erd dlmZ d d	lmZmZ d d
lmZm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z*m+Z+m,Z,m-Z- d dlm.Z. dd Z/edddddddddddddddddddddddddddddddddddddddddddddddd d!d dddd"d!dd#d!d$d%d&dd'd(!d)d*Z0eddddddddddddddddddddddddddddd+ddddddddd,ddddddddd d!d dddd"d!dd#d!d$d%d&dd-d(!d.d*Z0eddddddddddddddddddddddddddddd/dddddddddd,dddddddd d!d dddd"d!dd#d!d$d%d&dd-d(!d0d*Z0e/dd,d,d1d,d,dd'd2d3d*Z0edddddddddddddddddddddddddddddddddddddddddddddddd d!d dddd"d!dd#d!d$d%d&dd4d(!d5d6Z1eddddddddddddddddddddddddddddd+ddddddddd,ddddddddd d!d dddd"d!dd#d!d$d%d&dd-d(!d7d6Z1eddddddddddddddddddddddddddddd/dddddddddd,dddddddd d!d dddd"d!dd#d!d$d%d&dd-d(!d8d6Z1e/dd,d,d1d,d,dd4d2d9d6Z1ed:ed;Z2d<d=d>d?d@Z3dAdBdCdCdDdEdFZ4dd,dCdGdHdIdJZ5eddddddddddddddddddddddddddddddddddK!ddddddddddddddddd!d!d!d!dLdd dMdMd#d#d#d#d&ddd"d%d#ddNdO%dPdQZ6edddddddddddddddddddddddddddddddddR ddddddddddddddd,dd!d!d!d!dLdd dMdMd#d#d#d#d&ddd"d%d#ddSdO%dTdQZ6edddddddddddddddddddddddddddddddddU dddddddddddddd,ddd!d!d!d!dLdd dMdMd#d#d#d#d&ddd"d%d#ddSdO%dVdQZ6e3ej6ddddddddddddddddddddddddddddddddddK!ddddddddddddddddd!d!d!d!dLdd dMdMd#d#d#d#d&ddd"d%d#ddWdO%dXdQZ6dS )Y    )annotationsN)TYPE_CHECKINGAnyCallableHashableIterableTypeVaroverload)	broadcast)dataarray_plot)_easy_facetgrid)_add_colorbar_get_nice_quiver_magnitude_infer_meta_data_process_cmap_cbar_kwargsget_axis)Axes)LineCollectionPathCollection)Colormap	Normalize)Quiver)	ArrayLike	DataArray)Dataset)AspectOptionsExtendOptionsHueStyleOptionsScaleOptions)	FacetGridc              $     s   d} j  d|  _ tj ddd d d d d d d d d d d d d ddd d d d d d d d d d d d d d dddd	d	d	d	d	d
d	d	dddddddddddddddddddddddd! fdd}|`|S )Na  
    Parameters
    ----------

    ds : Dataset
    x : Hashable or None, optional
        Variable name for x-axis.
    y : Hashable or None, optional
        Variable name for y-axis.
    u : Hashable or None, optional
        Variable name for the *u* velocity (in *x* direction).
        quiver/streamplot plots only.
    v : Hashable or None, optional
        Variable name for the *v* velocity (in *y* direction).
        quiver/streamplot plots only.
    hue: Hashable or None, optional
        Variable by which to color scatter points or arrows.
    hue_style: {'continuous', 'discrete'} or None, optional
        How to use the ``hue`` variable:

        - ``'continuous'`` -- continuous color scale
          (default for numeric ``hue`` variables)
        - ``'discrete'`` -- a color for each unique value, using the default color cycle
          (default for non-numeric ``hue`` variables)

    row : Hashable or None, optional
        If passed, make row faceted plots on this dimension name.
    col : Hashable or None, optional
        If passed, make column faceted plots on this dimension name.
    col_wrap : int, optional
        Use together with ``col`` to wrap faceted plots.
    ax : matplotlib axes object or None, optional
        If ``None``, use the current axes. Not applicable when using facets.
    figsize : Iterable[float] or None, optional
        A tuple (width, height) of the figure in inches.
        Mutually exclusive with ``size`` and ``ax``.
    size : scalar, optional
        If provided, create a new figure for the plot with the given size.
        Height (in inches) of each plot. See also: ``aspect``.
    aspect : "auto", "equal", scalar or None, optional
        Aspect ratio of plot, so that ``aspect * size`` gives the width in
        inches. Only used if a ``size`` is provided.
    sharex : bool or None, optional
        If True all subplots share the same x-axis.
    sharey : bool or None, optional
        If True all subplots share the same y-axis.
    add_guide: bool or None, optional
        Add a guide that depends on ``hue_style``:

        - ``'continuous'`` -- build a colorbar
        - ``'discrete'`` -- build a legend

    subplot_kws : dict or None, optional
        Dictionary of keyword arguments for Matplotlib subplots
        (see :py:meth:`matplotlib:matplotlib.figure.Figure.add_subplot`).
        Only applies to FacetGrid plotting.
    cbar_kwargs : dict, optional
        Dictionary of keyword arguments to pass to the colorbar
        (see :meth:`matplotlib:matplotlib.figure.Figure.colorbar`).
    cbar_ax : matplotlib axes object, optional
        Axes in which to draw the colorbar.
    cmap : matplotlib colormap name or colormap, optional
        The mapping from data values to color space. Either a
        Matplotlib colormap name or object. If not provided, this will
        be either ``'viridis'`` (if the function infers a sequential
        dataset) or ``'RdBu_r'`` (if the function infers a diverging
        dataset).
        See :doc:`Choosing Colormaps in Matplotlib <matplotlib:tutorials/colors/colormaps>`
        for more information.

        If *seaborn* is installed, ``cmap`` may also be a
        `seaborn color palette <https://seaborn.pydata.org/tutorial/color_palettes.html>`_.
        Note: if ``cmap`` is a seaborn color palette,
        ``levels`` must also be specified.
    vmin : float or None, optional
        Lower value to anchor the colormap, otherwise it is inferred from the
        data and other keyword arguments. When a diverging dataset is inferred,
        setting `vmin` or `vmax` will fix the other by symmetry around
        ``center``. Setting both values prevents use of a diverging colormap.
        If discrete levels are provided as an explicit list, both of these
        values are ignored.
    vmax : float or None, optional
        Upper value to anchor the colormap, otherwise it is inferred from the
        data and other keyword arguments. When a diverging dataset is inferred,
        setting `vmin` or `vmax` will fix the other by symmetry around
        ``center``. Setting both values prevents use of a diverging colormap.
        If discrete levels are provided as an explicit list, both of these
        values are ignored.
    norm : matplotlib.colors.Normalize, optional
        If ``norm`` has ``vmin`` or ``vmax`` specified, the corresponding
        kwarg must be ``None``.
    infer_intervals: bool | None
        If True the intervals are infered.
    center : float, optional
        The value at which to center the colormap. Passing this value implies
        use of a diverging colormap. Setting it to ``False`` prevents use of a
        diverging colormap.
    robust : bool, optional
        If ``True`` and ``vmin`` or ``vmax`` are absent, the colormap range is
        computed with 2nd and 98th percentiles instead of the extreme values.
    colors : str or array-like of color-like, optional
        A single color or a list of colors. The ``levels`` argument
        is required.
    extend : {'neither', 'both', 'min', 'max'}, optional
        How to draw arrows extending the colorbar beyond its limits. If not
        provided, ``extend`` is inferred from ``vmin``, ``vmax`` and the data limits.
    levels : int or array-like, optional
        Split the colormap (``cmap``) into discrete color intervals. If an integer
        is provided, "nice" levels are chosen based on the data range: this can
        imply that the final number of levels is not exactly the expected one.
        Setting ``vmin`` and/or ``vmax`` with ``levels=N`` is equivalent to
        setting ``levels=np.linspace(vmin, vmax, N)``.
    **kwargs : optional
        Additional keyword arguments to wrapped Matplotlib function.
    
)
__module____name____qualname____doc__)assignedT)xyuvhue	hue_stylerowcolcol_wrapaxfigsizesizeaspectsharexsharey	add_guidesubplot_kwscbar_kwargscbar_axcmapvminvmaxnorminfer_intervalscenterrobustcolorsextendlevelsr   r   Hashable | Noner   
int | NoneAxes | NoneIterable[float] | Nonefloat | Noner   boolbool | Nonedict[str, Any] | Nonestr | Colormap | NoneNormalize | Nonestr | ArrayLike | Noner   ArrayLike | None)!dsargsr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   kwargsreturnc          *        sF  |rd} |d kst |d }t|dkr<|d ks4t |d }t|dkr\|d ksTt |d }t|dkr||d kstt |d }t|dkr|d kst |d }t|dkrt| ntj| tdd ~|d	d
}!|!r|d}"nt| |||||jd}"|"d }|s|rTt	 
 }#t j |#d< | |#d< dD ]}$|#|$= q2tf ddi|#|S |dd }t||||
}
|dkr|d k	r|!r|"d }|"d  nt| | jft	 \ } fdddD }%ni }%|d k	s|d k	rjdkrtdf | |||
|||||%d	|}&|!r |&S |"dd r>|
|"d |"dd r\|
|"d |"d r||
j|&|"d d d! |"d" r|d kri n|}d#|kr|"d d |d#< t|&|
||  |"d$ rt| | | | }'| | jd%d&}(|
j|&d'd(|'|' d)|( d*d+d, jdkr,| |  })n| |  })|
|) |&S )-NzYUsing positional arguments is deprecated for plot methods, use keyword arguments instead.r                  )
stacklevel_is_facetgridF	meta_data)funcnamer,   plotfuncdata)r[   rR   rP   kindZdatasetr1   Z
continuousr8   cmap_paramsc                   s   i | ]}| | qS  ra   ).0vvr`   ra   </tmp/pip-unpacked-wheel-h316xyqg/xarray/plot/dataset_plot.py
<dictcomp>   s     z0_dsplot.<locals>.newplotfunc.<locals>.<dictcomp>)r;   r<   r=   r:   )quiver
streamplotz5u, v are only allowed for quiver or streamplot plots.)	rP   r'   r(   r0   r)   r*   r+   r,   r`   ZxlabelZylabel
add_legendZ	hue_label)Zhandlestitleadd_colorbarlabelZadd_quiverkeyunits g333333?g?r!   Efigure)XYUrl   ZlabelposZcoordinates)AssertionErrorlen
ValueErrorwarningswarnDeprecationWarningpopr   r#   localscopyglobalsr   r   r   valuesgetZ
set_xlabelZ
set_ylabelZlegendr   r   attrsZ	quiverkeyZ_title_for_slice	set_title)*rP   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rQ   rR   msgrZ   r[   allargsargZcmap_params_subsetZ	primitiveZ	magnituderm   rj   r]   rd   re   newplotfunc   s    &
      


 
 






z_dsplot.<locals>.newplotfunc)r%   	functoolswraps__wrapped__)r]   Z	commondocr   ra   r   re   _dsplot%   sN    u X r   T)r'   r(   r)   r*   r+   r,   r.   r-   r0   r1   r2   r/   r4   r5   r3   r7   r6   r8   r9   r;   r<   r=   r>   r?   rC   r@   rA   rB   r:   r   r   rD   r   NonerF   rG   rH   rE   rI   r   rK   rJ   rM   rO   rN   r   rL   r   )!rP   rQ   r'   r(   r)   r*   r+   r,   r.   r-   r0   r1   r2   r/   r4   r5   r3   r7   r6   r8   r9   r;   r<   r=   r>   r?   rC   r@   rA   rB   r:   rR   rS   c                 O  s   d S Nra    rP   r'   r(   r)   r*   r+   r,   r.   r-   r0   r1   r2   r/   r4   r5   r3   r7   r6   r8   r9   r;   r<   r=   r>   r?   rC   r@   rA   rB   r:   rQ   rR   ra   ra   re   rg   C  s    #rg   )r'   r(   r)   r*   r+   r,   r-   r0   r1   r2   r/   r4   r5   r3   r7   r6   r8   r9   r;   r<   r=   r>   r?   rC   r@   rA   rB   r:   r   zFacetGrid[Dataset]c                 O  s   d S r   ra   r   ra   ra   re   rg   i  s    #)r'   r(   r)   r*   r+   r,   r.   r0   r1   r2   r/   r4   r5   r3   r7   r6   r8   r9   r;   r<   r=   r>   r?   rC   r@   rA   rB   r:   c                 O  s   d S r   ra   r   ra   ra   re   rg     s    #r   )rP   r'   r(   r0   r)   r*   rR   rS   c                 K  s   ddl }|dks(|dks(|dks(|dkr0tdt| | | | | | | | \}}	}
}|j|	j|
j|jg}|d}|d}|r|| | j |d s|j|d|d|d< |d	 |d
d |j	|||}|S )zaQuiver plot of Dataset variables.

    Wraps :py:func:`matplotlib:matplotlib.pyplot.quiver`.
    r   Nz)Must specify x, y, u, v for quiver plots.r+   r`   r=   r;   r<   r,   ZpivotZmiddle)

matplotlibrv   r
   r~   rz   appendrA   r   
setdefaultrg   )rP   r'   r(   r0   r)   r*   rR   mpldxdydudvrQ   r+   r`   hdlra   ra   re   rg     s$     &

 
r   c                 O  s   d S r   ra   r   ra   ra   re   rh     s    #rh   c                 O  s   d S r   ra   r   ra   ra   re   rh     s    #c                 O  s   d S r   ra   r   ra   ra   re   rh   )  s    #c                 K  s  ddl }|dks(|dks(|dks(|dkr0tdt| | jdkrP| | jd nd}t| | jdkrt| | jd nd}	|dk	r|	dkrt| | j|h }
t|
dkrtt|
}	|	dk	r|dkrt| | j|	h }t|dkrtt|}t| | | | | | | | \}}}}|dk	rZ|	dk	rZ||	|}||	|}||	|}||	|}|j	|j	|j	|j	g}|
d}|
d}|r| | j	|d< |d s|j|
d	|
d
|d< |
d |j|||}|jS )zjPlot streamlines of Dataset variables.

    Wraps :py:func:`matplotlib:matplotlib.pyplot.streamplot`.
    r   Nz-Must specify x, y, u, v for streamplot plots.rT   r+   r`   colorr=   r;   r<   r,   )r   rv   ru   Zdimssetnextiterr
   Z	transposer~   rz   rA   r   rh   lines)rP   r'   r(   r0   r)   r*   rR   r   ZxdimZydimZydimsZxdimsr   r   r   r   rQ   r+   r`   r   ra   ra   re   rh   O  s@     $$&


 
F)boundr   zCallable[[F], F])dataarray_plotfuncrS   c                   sb   | j }|dkrtdd}d}||kr<|||dd n| t| ddd fd	d
}|S )a  
    Add a common docstring by re-using the DataArray one.

    TODO: Reduce code duplication.

    * The goal is to reduce code duplication by moving all Dataset
      specific plots to the DataArray side and use this thin wrapper to
      handle the conversion between Dataset and DataArray.
    * Improve docstring handling, maybe reword the DataArray versions to
      explain Datasets better.

    Parameters
    ----------
    dataarray_plotfunc : Callable
        Function that returns a finished plot primitive.
    Nz*DataArray plot method requires a docstringz:
    Parameters
    ----------
    darray : DataArray
    z

    The `y` DataArray will be used as base, any other variables are added as coords.

    Parameters
    ----------
    ds : Dataset
    ZdarrayrP   r   )dataset_plotfuncrS   c                   s
    | _ | S r   )r%   )r   Zds_docra   re   wrapper  s    z'_update_doc_to_dataset.<locals>.wrapper)r%   NotImplementedErrorreplacer   r   )r   Zda_docZda_strZds_strr   ra   r   re   _update_doc_to_dataset  s    	r   strztuple[Any, ...]zdict[str, Any])
plotmethodrQ   rR   rS   c                 C  sH   ddl m} ttt| j| j||j	 }|
|di  |S )Nr   r   rR   )xarray.core.dataarrayr   dictinspect	signaturegetattrplotbind	argumentsitemsupdaterz   )r   rQ   rR   r   locals_ra   ra   re   _normalize_args  s     r   r   )rP   r(   r   rS   c           
      C  s~   ddl m} t| j}ddddddd	d
h}| |@ }|D ](}|| }| j|dk	r:| | ||< q:| | | }	||	|dS )z.Create a temporary datarray with extra coords.r   r   r'   z
markersizer+   r-   r.   r)   r*   N)coords)r   r   r   r   keysZ	data_varsr   Zbroadcast_like)
rP   r(   r   r   r   Zvalid_coord_kwargsZcoord_kwargskkeyZ_yra   ra   re   _temp_dataarray  s    
r   )!r'   r(   r   r+   r,   r   	linewidthr1   r2   r3   r0   r-   r.   r/   	xincrease	yincreaseri   rk   
add_labels	add_titler7   xscaleyscalexticksyticksxlimylimr:   r;   r<   r=   rB   rC   zbool | Iterable[bool]r   r   )%rP   rQ   r'   r(   r   r+   r,   r   r   r1   r2   r3   r0   r-   r.   r/   r   r   ri   rk   r   r   r7   r   r   r   r   r   r   r:   r;   r<   r=   rB   rC   rR   rS   c       !   $      O  s   d S r   ra   $rP   r'   r(   r   r+   r,   r   r   r1   r2   r3   r0   r-   r.   r/   r   r   ri   rk   r   r   r7   r   r   r   r   r   r   r:   r;   r<   r=   rB   rC   rQ   rR   ra   ra   re   scatter  s    'r   ) r'   r(   r   r+   r,   r   r   r1   r2   r3   r0   r-   r/   r   r   ri   rk   r   r   r7   r   r   r   r   r   r   r:   r;   r<   r=   rB   rC   zFacetGrid[DataArray]c       !   $      O  s   d S r   ra   r   ra   ra   re   r     s    ') r'   r(   r   r+   r,   r   r   r1   r2   r3   r0   r.   r/   r   r   ri   rk   r   r   r7   r   r   r   r   r   r   r:   r;   r<   r=   rB   rC   c       !   $      O  s   d S r   ra   r   ra   ra   re   r   B  s    'z%PathCollection | FacetGrid[DataArray]c       !   &      O  s@   t  }$|$d= |$|$di  t| ||$}%|%jj|$dd|$S )z7Scatter plot Dataset data variables against each other.rP   rR   rQ   ra   )r{   r   rz   r   r   r   )&rP   r'   r(   r   r+   r,   r   r   r1   r2   r3   r0   r-   r.   r/   r   r   ri   rk   r   r   r7   r   r   r   r   r   r   r:   r;   r<   r=   rB   rC   rQ   rR   r   dara   ra   re   r   l  s
    ()7
__future__r   r   r   rw   typingr   r   r   r   r   r   r	   Zxarray.core.alignmentr
   Zxarray.plotr   Zxarray.plot.facetgridr   Zxarray.plot.utilsr   r   r   r   r   Zmatplotlib.axesr   Zmatplotlib.collectionsr   r   Zmatplotlib.colorsr   r   Zmatplotlib.quiverr   Znumpy.typingr   r   r   Zxarray.core.datasetr   Zxarray.core.typesr   r   r   r   r    r   rg   rh   r   r   r   r   r   ra   ra   ra   re   <module>   s  $   T%T%T%'T%T%T%?2\)\)\)