<s> import collections as col <EOL> import numpy as np <EOL> import matplotlib as mpl <EOL> import matplotlib . backends . backend_agg as mbb <EOL> from jmpy import common <EOL> from jmpy . plotting import components <EOL> def boxplot ( x , y , data = None , legend = None , marker = '<STR_LIT:o>' , <EOL> alpha = <NUM_LIT> , points = True , cumprob = False , yscale = '<STR_LIT>' , <EOL> cmap = '<STR_LIT:default>' , figsize = ( <NUM_LIT:12> , <NUM_LIT:6> ) , orderby = None , table = True , <EOL> fig = None , axes = None , cgrid = None , violin = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> ( x , y , _ , legend , _ , _ ) , data = components . create_df ( x , y , legend ) <EOL> df = data . copy ( ) <EOL> df = df . reset_index ( ) <EOL> df [ x ] = df [ x ] . astype ( '<STR_LIT:str>' ) <EOL> df [ y ] = df [ y ] . astype ( '<STR_LIT:float>' ) . dropna ( ) <EOL> if orderby : <EOL> temp = df . sort ( x ) <EOL> t = temp . groupby ( x ) [ orderby ] <EOL> map_of_x = col . OrderedDict ( ) <EOL> for mg in sorted ( t . groups ) : <EOL> g = t . get_group ( mg ) . reset_index ( ) <EOL> map_of_x [ mg ] = g [ orderby ] [ <NUM_LIT:0> ] <EOL> list_to_order = sorted ( [ value for value in map_of_x . values ( ) ] ) <EOL> order = [ ] <EOL> x_to_loc = { } <EOL> for k , v in map_of_x . items ( ) : <EOL> idx = list_to_order . index ( v ) <EOL> x_to_loc [ k ] = idx <EOL> order . append ( idx ) <EOL> min_ , max_ = np . min ( df [ y ] ) , np . max ( df [ y ] ) <EOL> if fig and not axes : <EOL> fig = fig <EOL> canvas = mbb . FigureCanvasAgg ( fig ) <EOL> axm , axc , axl , axt = components . get_axes ( fig ) <EOL> elif axes : <EOL> axm = axes <EOL> else : <EOL> fig = mpl . figure . Figure ( figsize = figsize , tight_layout = True ) <EOL> canvas = mbb . FigureCanvasAgg ( fig ) <EOL> axm , axc , axl , axt = components . create_axes ( cumprob , legend , table , fig = fig ) <EOL> if violin : <EOL> array = [ ] <EOL> for arr in sorted ( set ( df [ x ] ) ) : <EOL> array . append ( df [ df [ x ] == arr ] [ y ] ) <EOL> axm . violinplot ( array , showmedians = True ) <EOL> else : <EOL> if orderby : <EOL> df . boxplot ( column = y , by = x , ax = axm , showfliers = False , <EOL> positions = order , fontsize = <NUM_LIT:8> , ** kwargs ) <EOL> else : <EOL> df . boxplot ( column = y , by = x , ax = axm , showfliers = False , fontsize = <NUM_LIT:8> , ** kwargs ) <EOL> unique_groups = set ( df [ x ] ) <EOL> nonan_grps = [ ] <EOL> for group in unique_groups : <EOL> if '<STR_LIT>' not in group : <EOL> nonan_grps . append ( group ) <EOL> if legend : <EOL> if cgrid is None : <EOL> cgrid = common . colors . colormap ( df [ legend ] , kind = '<STR_LIT>' , cmap = cmap ) <EOL> legend_color = { } <EOL> for i , key in df [ legend ] . iteritems ( ) : <EOL> legend_color [ key ] = cgrid [ i ] <EOL> if not axes : <EOL> axl = components . legend ( sorted ( list ( legend_color . items ( ) ) ) , axl ) <EOL> axl . set_title ( legend , loc = '<STR_LIT:left>' ) <EOL> groups = sorted ( nonan_grps ) <EOL> for j , val in enumerate ( groups ) : <EOL> ys = df [ y ] [ df [ x ] == val ] <EOL> if orderby : <EOL> pos = x_to_loc [ val ] <EOL> xs = np . random . normal ( pos , <NUM_LIT> , size = len ( ys ) ) <EOL> else : <EOL> xs = np . random . normal ( j + <NUM_LIT:1> , <NUM_LIT> , size = len ( ys ) ) <EOL> if points : <EOL> if legend or cgrid is not None : <EOL> cs = cgrid [ df [ x ] == val ] <EOL> axm . scatter ( xs , ys . values , color = cs , marker = marker , alpha = alpha , <EOL> linewidths = <NUM_LIT:1> , ** kwargs ) <EOL> else : <EOL> axm . scatter ( xs , ys . values , marker = marker , alpha = alpha , <EOL> linewidths = <NUM_LIT:1> , ** kwargs ) <EOL> if cumprob and not axes : <EOL> if legend : <EOL> cs = cgrid [ df [ x ] == val ] <EOL> axc = components . cumprob ( ys , axc , color = cs , alpha = alpha , swapxy = True ) <EOL> else : <EOL> axc = components . cumprob ( ys , axc , alpha = alpha , swapxy = True ) <EOL> axm . set_ylim ( min_ , max_ ) <EOL> axm . set_yscale ( yscale ) <EOL> axm . set_ylabel ( y ) <EOL> for label in axm . get_xticklabels ( ) : <EOL> label . set_rotation ( <NUM_LIT> ) <EOL> if cumprob and not axes : <EOL> axc . set_ylim ( min_ , max_ ) <EOL> axc . set_yscale ( yscale ) <EOL> axc . set_yticklabels ( [ ] , visible = False ) <EOL> for label in axc . get_xticklabels ( ) : <EOL> label . set_rotation ( <NUM_LIT> ) <EOL> if table and not axes : <EOL> components . datatable ( y , data , axt , by = x ) <EOL> axm . set_title ( '<STR_LIT>' ) <EOL> if axes : <EOL> return axm <EOL> fig . suptitle ( '<STR_LIT>' ) <EOL> return canvas . figure </s>
