<s> from __future__ import division <EOL> import inspect <EOL> import warnings <EOL> import itertools <EOL> import functools <EOL> import numpy as np <EOL> from . . core . formatting import format_item <EOL> from . utils import _determine_cmap_params , _infer_xy_labels <EOL> _FONTSIZE = '<STR_LIT>' <EOL> _NTICKS = <NUM_LIT:5> <EOL> def _nicetitle ( coord , value , maxchar , template ) : <EOL> """<STR_LIT>""" <EOL> prettyvalue = format_item ( value , quote_strings = False ) <EOL> title = template . format ( coord = coord , value = prettyvalue ) <EOL> if len ( title ) > maxchar : <EOL> title = title [ : ( maxchar - <NUM_LIT:3> ) ] + '<STR_LIT>' <EOL> return title <EOL> class FacetGrid ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data , col = None , row = None , col_wrap = None , <EOL> aspect = <NUM_LIT:1> , size = <NUM_LIT:3> , subplot_kws = None ) : <EOL> """<STR_LIT>""" <EOL> import matplotlib . pyplot as plt <EOL> rep_col = col is not None and not data [ col ] . to_index ( ) . is_unique <EOL> rep_row = row is not None and not data [ row ] . to_index ( ) . is_unique <EOL> if rep_col or rep_row : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if col and row : <EOL> single_group = False <EOL> nrow = len ( data [ row ] ) <EOL> ncol = len ( data [ col ] ) <EOL> nfacet = nrow * ncol <EOL> if col_wrap is not None : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> elif row and not col : <EOL> single_group = row <EOL> elif not row and col : <EOL> single_group = col <EOL> else : <EOL> raise ValueError ( <EOL> '<STR_LIT>' ) <EOL> if single_group : <EOL> nfacet = len ( data [ single_group ] ) <EOL> if col : <EOL> ncol = nfacet <EOL> if row : <EOL> ncol = <NUM_LIT:1> <EOL> if col_wrap is not None : <EOL> ncol = col_wrap <EOL> nrow = int ( np . ceil ( nfacet / ncol ) ) <EOL> subplot_kws = { } if subplot_kws is None else subplot_kws <EOL> cbar_space = <NUM_LIT:1> <EOL> figsize = ( ncol * size * aspect + cbar_space , nrow * size ) <EOL> fig , axes = plt . subplots ( nrow , ncol , <EOL> sharex = True , sharey = True , squeeze = False , <EOL> figsize = figsize , subplot_kw = subplot_kws ) <EOL> col_names = list ( data [ col ] . values ) if col else [ ] <EOL> row_names = list ( data [ row ] . values ) if row else [ ] <EOL> if single_group : <EOL> full = [ { single_group : x } for x in <EOL> data [ single_group ] . values ] <EOL> empty = [ None for x in range ( nrow * ncol - len ( full ) ) ] <EOL> name_dicts = full + empty <EOL> else : <EOL> rowcols = itertools . product ( row_names , col_names ) <EOL> name_dicts = [ { row : r , col : c } for r , c in rowcols ] <EOL> name_dicts = np . array ( name_dicts ) . reshape ( nrow , ncol ) <EOL> self . data = data <EOL> self . name_dicts = name_dicts <EOL> self . fig = fig <EOL> self . axes = axes <EOL> self . row_names = row_names <EOL> self . col_names = col_names <EOL> self . _single_group = single_group <EOL> self . _nrow = nrow <EOL> self . _row_var = row <EOL> self . _ncol = ncol <EOL> self . _col_var = col <EOL> self . _col_wrap = col_wrap <EOL> self . _x_var = None <EOL> self . _y_var = None <EOL> self . _cmap_extend = None <EOL> self . _mappables = [ ] <EOL> @ property <EOL> def _left_axes ( self ) : <EOL> return self . axes [ : , <NUM_LIT:0> ] <EOL> @ property <EOL> def _bottom_axes ( self ) : <EOL> return self . axes [ - <NUM_LIT:1> , : ] <EOL> def map_dataarray ( self , func , x , y , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cmap_kwargs = { '<STR_LIT>' : self . data . values , <EOL> '<STR_LIT>' : <NUM_LIT:7> if '<STR_LIT>' in func . __name__ else None , <EOL> '<STR_LIT>' : func . __name__ != '<STR_LIT>' , <EOL> } <EOL> cmap_args = inspect . getargspec ( _determine_cmap_params ) . args <EOL> cmap_kwargs . update ( ( a , kwargs [ a ] ) for a in cmap_args if a in kwargs ) <EOL> cmap_params = _determine_cmap_params ( ** cmap_kwargs ) <EOL> func_kwargs = kwargs . copy ( ) <EOL> func_kwargs . update ( cmap_params ) <EOL> func_kwargs . update ( { '<STR_LIT>' : False , '<STR_LIT>' : False } ) <EOL> x , y = _infer_xy_labels ( darray = self . data . loc [ self . name_dicts . flat [ <NUM_LIT:0> ] ] , <EOL> x = x , y = y ) <EOL> for d , ax in zip ( self . name_dicts . flat , self . axes . flat ) : <EOL> if d is not None : <EOL> subset = self . data . loc [ d ] <EOL> mappable = func ( subset , x , y , ax = ax , ** func_kwargs ) <EOL> self . _mappables . append ( mappable ) <EOL> self . _cmap_extend = cmap_params . get ( '<STR_LIT>' ) <EOL> self . _finalize_grid ( x , y ) <EOL> if kwargs . get ( '<STR_LIT>' , True ) : <EOL> self . add_colorbar ( ) <EOL> return self <EOL> def _finalize_grid ( self , * axlabels ) : <EOL> """<STR_LIT>""" <EOL> self . set_axis_labels ( * axlabels ) <EOL> self . set_titles ( ) <EOL> self . fig . tight_layout ( ) <EOL> for ax , namedict in zip ( self . axes . flat , self . name_dicts . flat ) : <EOL> if namedict is None : <EOL> ax . set_visible ( False ) <EOL> def add_colorbar ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs = kwargs . copy ( ) <EOL> if self . _cmap_extend is not None : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . _cmap_extend ) <EOL> if getattr ( self . data , '<STR_LIT:name>' , None ) is not None : <EOL> kwargs . setdefault ( '<STR_LIT:label>' , self . data . name ) <EOL> self . fig . colorbar ( self . _mappables [ - <NUM_LIT:1> ] , ax = list ( self . axes . flat ) , <EOL> ** kwargs ) <EOL> return self <EOL> def set_axis_labels ( self , x_var = None , y_var = None ) : <EOL> """<STR_LIT>""" <EOL> if x_var is not None : <EOL> self . _x_var = x_var <EOL> self . set_xlabels ( x_var ) <EOL> if y_var is not None : <EOL> self . _y_var = y_var <EOL> self . set_ylabels ( y_var ) <EOL> return self <EOL> def set_xlabels ( self , label = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if label is None : <EOL> label = self . _x_var <EOL> for ax in self . _bottom_axes : <EOL> ax . set_xlabel ( label , ** kwargs ) <EOL> return self <EOL> def set_ylabels ( self , label = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if label is None : <EOL> label = self . _y_var <EOL> for ax in self . _left_axes : <EOL> ax . set_ylabel ( label , ** kwargs ) <EOL> return self <EOL> def set_titles ( self , template = "<STR_LIT>" , maxchar = <NUM_LIT:30> , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> import matplotlib as mpl <EOL> kwargs [ "<STR_LIT:size>" ] = kwargs . pop ( "<STR_LIT:size>" , mpl . rcParams [ "<STR_LIT>" ] ) <EOL> nicetitle = functools . partial ( _nicetitle , maxchar = maxchar , <EOL> template = template ) <EOL> if self . _single_group : <EOL> for d , ax in zip ( self . name_dicts . flat , self . axes . flat ) : <EOL> if d is not None : <EOL> coord , value = list ( d . items ( ) ) . pop ( ) <EOL> title = nicetitle ( coord , value , maxchar = maxchar ) <EOL> ax . set_title ( title , ** kwargs ) <EOL> else : <EOL> for ax , row_name in zip ( self . axes [ : , - <NUM_LIT:1> ] , self . row_names ) : <EOL> title = nicetitle ( coord = self . _row_var , value = row_name , <EOL> maxchar = maxchar ) <EOL> ax . annotate ( title , xy = ( <NUM_LIT> , <NUM_LIT> ) , xycoords = "<STR_LIT>" , <EOL> rotation = <NUM_LIT> , ha = "<STR_LIT:left>" , va = "<STR_LIT>" , ** kwargs ) <EOL> for ax , col_name in zip ( self . axes [ <NUM_LIT:0> , : ] , self . col_names ) : <EOL> title = nicetitle ( coord = self . _col_var , value = col_name , <EOL> maxchar = maxchar ) <EOL> ax . set_title ( title , ** kwargs ) <EOL> return self <EOL> def set_ticks ( self , max_xticks = _NTICKS , max_yticks = _NTICKS , <EOL> fontsize = _FONTSIZE ) : <EOL> """<STR_LIT>""" <EOL> from matplotlib . ticker import MaxNLocator <EOL> x_major_locator = MaxNLocator ( nbins = max_xticks ) <EOL> y_major_locator = MaxNLocator ( nbins = max_yticks ) <EOL> for ax in self . axes . flat : <EOL> ax . xaxis . set_major_locator ( x_major_locator ) <EOL> ax . yaxis . set_major_locator ( y_major_locator ) <EOL> for tick in itertools . chain ( ax . xaxis . get_major_ticks ( ) , <EOL> ax . yaxis . get_major_ticks ( ) ) : <EOL> tick . label . set_fontsize ( fontsize ) <EOL> return self <EOL> def map ( self , func , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> import matplotlib . pyplot as plt <EOL> for ax , namedict in zip ( self . axes . flat , self . name_dicts . flat ) : <EOL> if namedict is not None : <EOL> data = self . data . loc [ namedict ] <EOL> plt . sca ( ax ) <EOL> innerargs = [ data [ a ] . values for a in args ] <EOL> mappable = func ( * innerargs , ** kwargs ) <EOL> self . _mappables . append ( mappable ) <EOL> self . _finalize_grid ( * args [ : <NUM_LIT:2> ] ) <EOL> return self </s>
