<s> import itertools <EOL> import matplotlib as mpl <EOL> import matplotlib . backends . backend_agg as mbb <EOL> import matplotlib . gridspec as gs <EOL> from jmpy . plotting import components <EOL> from jmpy import common <EOL> def grid ( rows = None , cols = None , data = None , chart = None , args = None , figsize = ( <NUM_LIT:8> , <NUM_LIT:8> ) , legend = None , <EOL> cmap = '<STR_LIT:default>' , colorbar = False ) : <EOL> """<STR_LIT>""" <EOL> df = data . copy ( ) <EOL> rows_array , cols_array = [ ] , [ ] <EOL> try : <EOL> cols_array = sorted ( set ( df [ cols ] ) ) <EOL> except ( KeyError , NameError , ValueError ) : <EOL> pass <EOL> try : <EOL> rows_array = sorted ( set ( df [ rows ] ) ) <EOL> except ( KeyError , NameError , ValueError ) : <EOL> pass <EOL> numcols = len ( cols_array ) if cols_array else <NUM_LIT:1> <EOL> numrows = len ( rows_array ) if rows_array else <NUM_LIT:1> <EOL> numcols += <NUM_LIT:1> <EOL> numrows += <NUM_LIT:1> <EOL> if legend : <EOL> numcols += <NUM_LIT:1> <EOL> fig = mpl . figure . Figure ( figsize = figsize , tight_layout = True ) <EOL> canvas = mbb . FigureCanvasAgg ( fig ) <EOL> if len ( cols_array ) : <EOL> wrs = [ <NUM_LIT:1> ] + [ <NUM_LIT:5> for i in cols_array ] <EOL> else : <EOL> wrs = [ <NUM_LIT:1> , <NUM_LIT:5> ] <EOL> if legend : <EOL> wrs += [ <NUM_LIT:1> ] <EOL> if rows_array : <EOL> hrs = [ <NUM_LIT:1> ] + [ <NUM_LIT:5> for i in rows_array ] <EOL> else : <EOL> hrs = [ <NUM_LIT:1> , <NUM_LIT:5> ] <EOL> grid = gs . GridSpec ( numrows , numcols , width_ratios = wrs , <EOL> height_ratios = hrs ) <EOL> if len ( cols_array ) > <NUM_LIT:0> : <EOL> x = <NUM_LIT:1> if len ( rows_array ) else <NUM_LIT:1> <EOL> for i , val in enumerate ( cols_array , start = x ) : <EOL> ax = fig . add_subplot ( grid [ <NUM_LIT:0> , i ] ) <EOL> ax . text ( <NUM_LIT> , <NUM_LIT> , val ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> p = mpl . patches . Rectangle ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> fill = True , transform = ax . transAxes , clip_on = True , <EOL> fc = '<STR_LIT>' ) <EOL> ax . add_patch ( p ) <EOL> if len ( rows_array ) > <NUM_LIT:0> : <EOL> y = <NUM_LIT:1> if len ( cols_array ) else <NUM_LIT:1> <EOL> for i , val in enumerate ( rows_array , start = y ) : <EOL> ax = fig . add_subplot ( grid [ i , <NUM_LIT:0> ] ) <EOL> ax . text ( <NUM_LIT> , <NUM_LIT> , val , rotation = <NUM_LIT> ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> p = mpl . patches . Rectangle ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> fill = True , transform = ax . transAxes , clip_on = True , <EOL> fc = '<STR_LIT>' ) <EOL> ax . add_patch ( p ) <EOL> charts = list ( itertools . product ( cols_array , rows_array ) ) <EOL> if not list ( charts ) : <EOL> try : <EOL> charts = list ( itertools . zip_longest ( cols_array , rows_array ) ) <EOL> except AttributeError : <EOL> charts = list ( itertools . izip_longest ( cols_array , rows_array ) ) <EOL> if legend : <EOL> cgrid = common . colors . colormap ( df [ legend ] , kind = '<STR_LIT>' , cmap = cmap ) <EOL> for x , y in charts : <EOL> tdf = df [ df [ cols ] == x ] if ( x and cols ) else df <EOL> tdf = tdf [ tdf [ rows ] == y ] if ( y and rows ) else tdf <EOL> if tdf . size == <NUM_LIT:0> : <EOL> continue <EOL> tc = None <EOL> if legend : <EOL> tc = cgrid [ df [ cols ] == x ] if ( x and cols ) else cgrid <EOL> tc = tc [ df [ rows ] == y ] if ( y and rows ) else tc <EOL> tc = tc . reset_index ( drop = True ) <EOL> ax = fig . add_subplot ( grid [ rows_array . index ( y ) + <NUM_LIT:1> if y else <NUM_LIT:1> , <EOL> cols_array . index ( x ) + <NUM_LIT:1> if x else <NUM_LIT:1> ] ) <EOL> if legend : <EOL> chart ( data = tdf , axes = ax , cgrid = tc , legend = legend , ** args ) <EOL> else : <EOL> chart ( data = tdf , axes = ax , cgrid = tc , ** args ) <EOL> if legend : <EOL> legend_color = { } <EOL> for i , key in df [ legend ] . iteritems ( ) : <EOL> legend_color [ key ] = cgrid [ i ] <EOL> axl = fig . add_subplot ( grid [ <NUM_LIT:1> , numcols - <NUM_LIT:1> ] ) <EOL> axl = components . legend ( sorted ( list ( legend_color . items ( ) ) ) , axl ) <EOL> axl . set_title ( legend , loc = '<STR_LIT:left>' ) <EOL> fig . suptitle ( '<STR_LIT>' ) <EOL> return canvas . figure </s>
