<s> 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 histogram ( x , data = None , legend = None , figsize = ( <NUM_LIT:12> , <NUM_LIT:6> ) , <EOL> xscale = '<STR_LIT>' , yscale = '<STR_LIT>' , cmap = '<STR_LIT:default>' , <EOL> alpha = <NUM_LIT:0.5> , cumprob = False , marker = '<STR_LIT:.>' , bins = <NUM_LIT> , <EOL> table = True , fig = None , axes = None , cgrid = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> ( x , _ , _ , legend , _ , _ ) , data = components . create_df ( x , None , legend ) <EOL> df = data . copy ( ) <EOL> df = df . reset_index ( ) <EOL> df [ x ] = df [ x ] . astype ( '<STR_LIT:float>' ) . dropna ( ) <EOL> min_ , max_ = np . min ( df [ x ] ) , np . max ( df [ x ] ) <EOL> binlist = np . linspace ( min_ , max_ , bins ) <EOL> if fig : <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 table and not axes : <EOL> axt = components . datatable ( x , data , axt , by = legend ) <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> for group in sorted ( set ( df [ legend ] ) ) : <EOL> axm . hist ( np . asarray ( df [ df [ legend ] == group ] [ x ] ) , <EOL> alpha = alpha , <EOL> bins = binlist , <EOL> color = legend_color [ group ] , <EOL> label = str ( group ) , <EOL> ** kwargs ) <EOL> if cumprob and not axes : <EOL> axc = components . cumprob ( df [ df [ legend ] == group ] [ x ] , <EOL> axc , <EOL> color = legend_color [ group ] , <EOL> marker = marker , <EOL> alpha = alpha ) <EOL> else : <EOL> axm . hist ( np . asarray ( df [ x ] ) , <EOL> alpha = alpha , <EOL> bins = binlist , <EOL> ** kwargs ) <EOL> if cumprob : <EOL> axc = components . cumprob ( df [ x ] , axc , marker = marker , alpha = alpha ) <EOL> axm . set_xlim ( min_ , max_ ) <EOL> axm . set_xscale ( xscale ) <EOL> axm . set_yscale ( yscale ) <EOL> axm . set_xlabel ( x ) <EOL> for label in axm . get_xticklabels ( ) : <EOL> label . set_rotation ( <NUM_LIT> ) <EOL> if cumprob : <EOL> axc . set_xlim ( min_ , max_ ) <EOL> axc . set_xscale ( xscale ) <EOL> axc . set_yticklabels ( [ ] , visible = False ) <EOL> for label in axc . get_xticklabels ( ) : <EOL> label . set_rotation ( <NUM_LIT> ) <EOL> if axes : <EOL> return axm <EOL> return canvas . figure </s>
