HOME


sh-3ll 1.0
DIR:/usr/local/lib/python3.6/site-packages/xarray/plot/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.6/site-packages/xarray/plot/__pycache__/facetgrid.cpython-36.pyc
3

���h)8�@stddlmZddlZddlZddlZddlZddlZddlm	Z	ddl
mZmZdZ
dZd	d
�ZGdd�de�ZdS)
�)�divisionN�)�format_item�)�_determine_cmap_params�_infer_xy_labels�small�cCs>t|dd�}|j||d�}t|�|kr:|d|d�d}|S)z>
    Put coord, value in template and truncate at maxchar
    F)Z
quote_strings)�coord�valueN�z...)r�format�len)r
r�maxchar�templateZprettyvalue�title�r�5/tmp/pip-build-5_djhm0z/xray/xarray/plot/facetgrid.py�
_nicetitles
rc@s�eZdZdZddd�Zedd��Zed	d
��Zdd�Zd
d�Z	dd�Z
d dd�Zd!dd�Zd"dd�Z
d#dd�Zeeefdd�Zdd�ZdS)$�	FacetGrida�
    Initialize the matplotlib figure and FacetGrid object.

    The :class:`FacetGrid` is an object that links a xarray DataArray to
    a matplotlib figure with a particular structure.

    In particular, :class:`FacetGrid` is used to draw plots with multiple
    Axes where each Axes shows the same relationship conditioned on
    different levels of some dimension. It's possible to condition on up to
    two variables by assigning variables to the rows and columns of the
    grid.

    The general approach to plotting here is called "small multiples",
    where the same kind of plot is repeated multiple times, and the
    specific use of small multiples to display the same relationship
    conditioned on one ore more other variables is often called a "trellis
    plot".

    The basic workflow is to initialize the :class:`FacetGrid` object with
    the DataArray and the variable names that are used to structure the grid.
    Then plotting functions can be applied to each subset by calling
    :meth:`FacetGrid.map_dataarray` or :meth:`FacetGrid.map`.

    Attributes
    ----------
    axes : numpy object array
        Contains axes in corresponding position, as returned from
        plt.subplots
    fig : matplotlib.Figure
        The figure containing all the axes
    name_dicts : numpy object array
        Contains dictionaries mapping coordinate names to values. None is
        used as a sentinel value for axes which should remain empty, ie.
        sometimes the bottom right grid

    Nrrc	sDddlj}�dk	o |�j�j}	�dk	o8|�j�j}
|	sB|
rJtd���r��r�d�t|��}t|��}||}
|dk	r�tjd�n(�r��r���n�r��r���ntd���r�t|��}
�r�|
}�r�d}|dk	r�|}tt	j
|
|��}|dkr�in|}d}||||||f}|j||ddd||d	�\}}��rLt|�j
�ng}��rdt|�j
�ng}��r��fd
d�|�j
D�}dd�t||t|��D�}||}n tj||�}��fd
d�|D�}t	j|�j||�}||_||_||_||_||_||_�|_||_�|_||_�|_||_d|_d|_ d|_!g|_"dS)a�
        Parameters
        ----------
        data : DataArray
            xarray DataArray to be plotted
        row, col : strings
            Dimesion names that define subsets of the data, which will be drawn
            on separate facets in the grid.
        col_wrap : int, optional
            "Wrap" the column variable at this width, so that the column facets
        aspect : scalar, optional
            Aspect ratio of each facet, so that ``aspect * size`` gives the
            width of each facet in inches
        size : scalar, optional
            Height (in inches) of each facet. See also: ``aspect``
        subplot_kws : dict, optional
            Dictionary of keyword arguments for matplotlib subplots

        rNzICoordinates used for faceting cannot contain repeated (nonunique) values.Fz4Ignoring col_wrap since both col and row were passedz4Pass a coordinate name as an argument for row or colrT)ZsharexZshareyZsqueeze�figsizeZ
subplot_kwcsg|]}�|i�qSrr)�.0�x)�single_grouprr�
<listcomp>�sz&FacetGrid.__init__.<locals>.<listcomp>cSsg|]}d�qS)Nr)rrrrrr�scsg|]\}}�|�|i�qSrr)r�r�c)�col�rowrrr�s)#�matplotlib.pyplot�pyplotZto_indexZ	is_unique�
ValueErrorr�warnings�warn�int�np�ceilZsubplots�list�values�range�	itertools�product�arrayZreshape�data�
name_dicts�fig�axes�	row_names�	col_names�
_single_groupZ_nrow�_row_varZ_ncol�_col_varZ	_col_wrap�_x_var�_y_var�_cmap_extend�
_mappables)�selfr-rrZcol_wrapZaspect�sizeZsubplot_kws�pltZrep_colZrep_rowZnrowZncolZnfacetZ
cbar_spacerr/r0r2r1�full�emptyr.Zrowcolsr)rrrr�__init__Hsv




zFacetGrid.__init__cCs|jdd�dfS)Nr)r0)r:rrr�
_left_axes�szFacetGrid._left_axescCs|jddd�fS)Nr���)r0)r:rrr�_bottom_axes�szFacetGrid._bottom_axesc
s |jjd|jkrdnd|jdkd�}tjt�j}|j�fdd�|D��tf|�}�j�}|j|�|jddd��t	|jj
|jjd	||d
�\}}xVt
|jj|jj�D]@\}	}
|	dk	r�|jj
|	}||||fd|
i|��}|jj|�q�W|jd�|_|j||��jd
d��r|j�|S)a+
        Apply a plotting function to a 2d facet's subset of the data.

        This is more convenient and less general than ``FacetGrid.map``

        Parameters
        ----------
        func : callable
            A plotting function with the same signature as a 2d xarray
            plotting method such as `xarray.plot.imshow`
        x, y : string
            Names of the coordinates to plot on x, y axes
        kwargs :
            additional keyword arguments to func

        Returns
        -------
        self : FacetGrid object

        Zcontour�N)Z	plot_dataZlevelsZfilledc3s"|]}|�kr|�|fVqdS)Nr)r�a)�kwargsrr�	<genexpr>�sz*FacetGrid.map_dataarray.<locals>.<genexpr>F)�add_colorbarZ
add_labelsr)Zdarrayr�y�ax�extendrGT)r-r(�__name__�inspectZ
getargspecr�args�update�copyr�locr.�flat�zipr0r9�append�getr8�_finalize_gridrG)
r:�funcrrHrEZcmap_kwargsZ	cmap_argsZcmap_paramsZfunc_kwargs�drIZsubset�mappabler)rEr�
map_dataarray�s*

zFacetGrid.map_dataarraycGsR|j|�|j�|jj�x0t|jj|jj�D]\}}|dkr0|jd�q0WdS)z$Finalize the annotations and layout.NF)	�set_axis_labels�
set_titlesr/Ztight_layoutrRr0rQr.Zset_visible)r:ZaxlabelsrI�namedictrrrrU�s

zFacetGrid._finalize_gridcKsl|j�}|jdk	r |jd|j�t|jdd�dk	rB|jd|jj�|jj|jdfdt	|j
j�i|��|S)zDraw a colorbar
        NrJ�name�labelrrIrA)rOr8�
setdefault�getattrr-r]r/Zcolorbarr9r'r0rQ)r:rErrrrGs
zFacetGrid.add_colorbarcCs4|dk	r||_|j|�|dk	r0||_|j|�|S)z>Set axis labels on the left column and bottom row of the grid.N)r6�set_xlabelsr7�set_ylabels)r:Zx_varZy_varrrrrZs

zFacetGrid.set_axis_labelscKs0|dkr|j}x|jD]}|j|f|�qW|S)z/Label the x axis on the bottom row of the grid.N)r6rBZ
set_xlabel)r:r^rErIrrrras
zFacetGrid.set_xlabelscKs0|dkr|j}x|jD]}|j|f|�qW|S)z0Label the y axis on the left column of the grid.N)r7r@Z
set_ylabel)r:r^rErIrrrrb s
zFacetGrid.set_ylabels�{coord} = {value}�c
	Ks0ddl}|jd|jd�|d<tjt||d�}|jr�x�t|jj	|j
j	�D]@\}}|dk	rHt|j��j�\}}	|||	|d�}
|j
|
f|�qHWn�xTt|j
dd�df|j�D]6\}}||j||d�}
|j|
fdddd
dd�|��q�WxFt|j
ddd�f|j�D](\}}||j||d�}
|j
|
f|��qW|S)a�
        Draw titles either above each facet or on the grid margins.

        Parameters
        ----------
        template : string
            Template for plot titles containing {coord} and {value}
        maxchar : int
            Truncate titles at maxchar
        kwargs : keyword args
            additional arguments to matplotlib.text

        Returns
        -------
        self: FacetGrid object

        rNr;zaxes.labelsize)rr)rr)r
rr�R���Q�?��?z
axes fractioni�left�center)ZxyZxycoordsZrotationZha�varA)rerf)Z
matplotlib�popZrcParams�	functools�partialrr3rRr.rQr0r'�items�	set_titler1r4Zannotater2r5)
r:rrrEZmplZ	nicetitlerWrIr
rrZrow_nameZcol_namerrrr[(s($$zFacetGrid.set_titlesc	Cs|ddlm}||d�}||d�}xV|jjD]J}|jj|�|jj|�x,tj|jj	�|jj	��D]}|j
j|�q`Wq*W|S)aZ
        Set and control tick behavior

        Parameters
        ----------
        max_xticks, max_yticks : int, optional
            Maximum number of labeled ticks to plot on x, y axes
        fontsize : string or int
            Font size as used by matplotlib text

        Returns
        -------
        self : FacetGrid object

        r)�MaxNLocator)Znbins)Zmatplotlib.tickerror0rQZxaxisZset_major_locatorZyaxisr*�chainZget_major_ticksr^Zset_fontsize)	r:Z
max_xticksZ
max_yticksZfontsizeroZx_major_locatorZy_major_locatorrIZtickrrr�	set_ticksYs

zFacetGrid.set_ticksc	s�ddlj}xdt|jj|jj�D]N\}}|dk	r|jj|�|j|��fdd�|D�}|||�}|j	j
|�qW|j|dd��|S)a6
        Apply a plotting function to each facet's subset of the data.

        Parameters
        ----------
        func : callable
            A plotting function that takes data and keyword arguments. It
            must plot to the currently active matplotlib Axes and take a
            `color` keyword argument. If faceting on the `hue` dimension,
            it must also take a `label` keyword argument.
        args : strings
            Column names in self.data that identify variables with data to
            plot. The data for each variable is passed to `func` in the
            order the variables are specified in the call.
        kwargs : keyword arguments
            All keyword arguments are passed to the plotting function.

        Returns
        -------
        self : FacetGrid object

        rNcsg|]}�|j�qSr)r()rrD)r-rrr�sz!FacetGrid.map.<locals>.<listcomp>r)rr rRr0rQr.r-rPZscar9rSrU)	r:rVrMrEr<rIr\Z	innerargsrXr)r-r�mapys


z
FacetGrid.map)NNNrrN)NN)N)N)rcrd)rK�
__module__�__qualname__�__doc__r?�propertyr@rBrYrUrGrZrarbr[�_NTICKS�	_FONTSIZErqrrrrrrr"s$
n9





1r)�
__future__rrLr"r*rkZnumpyr%Zcore.formattingr�utilsrrrxrwr�objectrrrrr�<module>s