<s> import numpy as np <EOL> import pandas as pd <EOL> import pandas . compat as compat <EOL> class TablePlotter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cell_width = <NUM_LIT> , cell_height = <NUM_LIT> , font_size = <NUM_LIT> ) : <EOL> self . cell_width = cell_width <EOL> self . cell_height = cell_height <EOL> self . font_size = font_size <EOL> def _shape ( self , df ) : <EOL> """<STR_LIT>""" <EOL> row , col = df . shape <EOL> return row + df . columns . nlevels , col + df . index . nlevels <EOL> def _get_cells ( self , left , right , vertical ) : <EOL> """<STR_LIT>""" <EOL> if vertical : <EOL> vcells = max ( sum ( [ self . _shape ( l ) [ <NUM_LIT:0> ] for l in left ] ) , <EOL> self . _shape ( right ) [ <NUM_LIT:0> ] ) <EOL> hcells = ( max ( [ self . _shape ( l ) [ <NUM_LIT:1> ] for l in left ] ) + <EOL> self . _shape ( right ) [ <NUM_LIT:1> ] ) <EOL> else : <EOL> vcells = max ( [ self . _shape ( l ) [ <NUM_LIT:0> ] for l in left ] + <EOL> [ self . _shape ( right ) [ <NUM_LIT:0> ] ] ) <EOL> hcells = sum ( [ self . _shape ( l ) [ <NUM_LIT:1> ] for l in left ] + <EOL> [ self . _shape ( right ) [ <NUM_LIT:1> ] ] ) <EOL> return hcells , vcells <EOL> def plot ( self , left , right , labels = None , vertical = True ) : <EOL> """<STR_LIT>""" <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . gridspec as gridspec <EOL> if not isinstance ( left , list ) : <EOL> left = [ left ] <EOL> left = [ self . _conv ( l ) for l in left ] <EOL> right = self . _conv ( right ) <EOL> hcells , vcells = self . _get_cells ( left , right , vertical ) <EOL> if vertical : <EOL> figsize = self . cell_width * hcells , self . cell_height * vcells <EOL> else : <EOL> figsize = self . cell_width * hcells , self . cell_height * vcells <EOL> fig = plt . figure ( figsize = figsize ) <EOL> if vertical : <EOL> gs = gridspec . GridSpec ( len ( left ) , hcells ) <EOL> max_left_cols = max ( [ self . _shape ( l ) [ <NUM_LIT:1> ] for l in left ] ) <EOL> max_left_rows = max ( [ self . _shape ( l ) [ <NUM_LIT:0> ] for l in left ] ) <EOL> for i , ( l , label ) in enumerate ( zip ( left , labels ) ) : <EOL> ax = fig . add_subplot ( gs [ i , <NUM_LIT:0> : max_left_cols ] ) <EOL> self . _make_table ( ax , l , title = label , <EOL> height = <NUM_LIT:1.0> / max_left_rows ) <EOL> ax = plt . subplot ( gs [ : , max_left_cols : ] ) <EOL> self . _make_table ( ax , right , title = '<STR_LIT>' , height = <NUM_LIT> / vcells ) <EOL> fig . subplots_adjust ( top = <NUM_LIT> , bottom = <NUM_LIT> , left = <NUM_LIT> , right = <NUM_LIT> ) <EOL> else : <EOL> max_rows = max ( [ self . _shape ( df ) [ <NUM_LIT:0> ] for df in left + [ right ] ] ) <EOL> height = <NUM_LIT:1.0> / np . max ( max_rows ) <EOL> gs = gridspec . GridSpec ( <NUM_LIT:1> , hcells ) <EOL> i = <NUM_LIT:0> <EOL> for l , label in zip ( left , labels ) : <EOL> sp = self . _shape ( l ) <EOL> ax = fig . add_subplot ( gs [ <NUM_LIT:0> , i : i + sp [ <NUM_LIT:1> ] ] ) <EOL> self . _make_table ( ax , l , title = label , height = height ) <EOL> i += sp [ <NUM_LIT:1> ] <EOL> ax = plt . subplot ( gs [ <NUM_LIT:0> , i : ] ) <EOL> self . _make_table ( ax , right , title = '<STR_LIT>' , height = height ) <EOL> fig . subplots_adjust ( top = <NUM_LIT> , bottom = <NUM_LIT> , left = <NUM_LIT> , right = <NUM_LIT> ) <EOL> return fig <EOL> def _conv ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( data , pd . Series ) : <EOL> if data . name is None : <EOL> data = data . to_frame ( name = '<STR_LIT>' ) <EOL> else : <EOL> data = data . to_frame ( ) <EOL> data = data . fillna ( '<STR_LIT>' ) <EOL> return data <EOL> def _insert_index ( self , data ) : <EOL> data = data . copy ( ) <EOL> idx_nlevels = data . index . nlevels <EOL> if idx_nlevels == <NUM_LIT:1> : <EOL> data . insert ( <NUM_LIT:0> , '<STR_LIT>' , data . index ) <EOL> else : <EOL> for i in range ( idx_nlevels ) : <EOL> data . insert ( i , '<STR_LIT>' . format ( i ) , <EOL> data . index . get_level_values ( i ) ) <EOL> col_nlevels = data . columns . nlevels <EOL> if col_nlevels > <NUM_LIT:1> : <EOL> col = data . columns . get_level_values ( <NUM_LIT:0> ) <EOL> values = [ data . columns . get_level_values ( i ) . values <EOL> for i in range ( <NUM_LIT:1> , col_nlevels ) ] <EOL> col_df = pd . DataFrame ( values ) <EOL> data . columns = col_df . columns <EOL> data = pd . concat ( [ col_df , data ] ) <EOL> data . columns = col <EOL> return data <EOL> def _make_table ( self , ax , df , title , height = None ) : <EOL> if df is None : <EOL> ax . set_visible ( False ) <EOL> return <EOL> import pandas . tools . plotting as plotting <EOL> idx_nlevels = df . index . nlevels <EOL> col_nlevels = df . columns . nlevels <EOL> df = self . _insert_index ( df ) <EOL> tb = plotting . table ( ax , df , loc = <NUM_LIT:9> ) <EOL> tb . set_fontsize ( self . font_size ) <EOL> if height is None : <EOL> height = <NUM_LIT:1.0> / ( len ( df ) + <NUM_LIT:1> ) <EOL> props = tb . properties ( ) <EOL> for ( r , c ) , cell in compat . iteritems ( props [ '<STR_LIT>' ] ) : <EOL> if c == - <NUM_LIT:1> : <EOL> cell . set_visible ( False ) <EOL> elif r < col_nlevels and c < idx_nlevels : <EOL> cell . set_visible ( False ) <EOL> elif r < col_nlevels or c < idx_nlevels : <EOL> cell . set_facecolor ( '<STR_LIT>' ) <EOL> cell . set_height ( height ) <EOL> ax . set_title ( title , size = self . font_size ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import matplotlib . pyplot as plt <EOL> p = TablePlotter ( ) <EOL> df1 = pd . DataFrame ( { '<STR_LIT:A>' : [ <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:12> ] , <EOL> '<STR_LIT:B>' : [ <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT:C>' : [ <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT:32> ] } ) <EOL> df2 = pd . DataFrame ( { '<STR_LIT:A>' : [ <NUM_LIT:10> , <NUM_LIT:12> ] , <EOL> '<STR_LIT:C>' : [ <NUM_LIT:30> , <NUM_LIT:32> ] } ) <EOL> p . plot ( [ df1 , df2 ] , pd . concat ( [ df1 , df2 ] ) , <EOL> labels = [ '<STR_LIT>' , '<STR_LIT>' ] , vertical = True ) <EOL> plt . show ( ) <EOL> df3 = pd . DataFrame ( { '<STR_LIT:X>' : [ <NUM_LIT:10> , <NUM_LIT:12> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:30> , <NUM_LIT:32> ] } ) <EOL> p . plot ( [ df1 , df3 ] , pd . concat ( [ df1 , df3 ] , axis = <NUM_LIT:1> ) , <EOL> labels = [ '<STR_LIT>' , '<STR_LIT>' ] , vertical = False ) <EOL> plt . show ( ) <EOL> idx = pd . MultiIndex . from_tuples ( [ ( <NUM_LIT:1> , '<STR_LIT:A>' ) , ( <NUM_LIT:1> , '<STR_LIT:B>' ) , ( <NUM_LIT:1> , '<STR_LIT:C>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT:A>' ) , ( <NUM_LIT:2> , '<STR_LIT:B>' ) , ( <NUM_LIT:2> , '<STR_LIT:C>' ) ] ) <EOL> col = pd . MultiIndex . from_tuples ( [ ( <NUM_LIT:1> , '<STR_LIT:A>' ) , ( <NUM_LIT:1> , '<STR_LIT:B>' ) ] ) <EOL> df3 = pd . DataFrame ( { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:10> ] } , <EOL> index = idx ) <EOL> df3 . columns = col <EOL> p . plot ( df3 , df3 , labels = [ '<STR_LIT>' ] ) <EOL> plt . show ( ) </s>
