<s> from __future__ import ( absolute_import , division , print_function , <EOL> unicode_literals ) <EOL> import numpy as np <EOL> from matplotlib . colors import rgb2hex <EOL> from . . utils . color import ColorHCL <EOL> from . legend import get_labels <EOL> from copy import deepcopy <EOL> from . . utils . exceptions import GgplotError <EOL> from . import palettes <EOL> import six <EOL> def hue_pal ( h = ( <NUM_LIT:0> + <NUM_LIT:15> , <NUM_LIT> + <NUM_LIT:15> ) , c = <NUM_LIT:100> , l = <NUM_LIT> , h_start = <NUM_LIT:0> , direction = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> c /= <NUM_LIT> <EOL> l /= <NUM_LIT> <EOL> hcl = ColorHCL ( ) <EOL> def func ( n ) : <EOL> y = deepcopy ( h ) <EOL> if ( y [ <NUM_LIT:1> ] - y [ <NUM_LIT:0> ] ) % <NUM_LIT> < <NUM_LIT:1> : <EOL> y = ( y [ <NUM_LIT:0> ] , y [ <NUM_LIT:1> ] - <NUM_LIT> / n ) <EOL> hues = [ ] <EOL> for x in np . linspace ( y [ <NUM_LIT:0> ] , y [ <NUM_LIT:1> ] , n ) : <EOL> hue = ( ( x + h_start ) % <NUM_LIT> ) * direction <EOL> hues . append ( rgb2hex ( hcl ( hue , c , l ) ) ) <EOL> return hues <EOL> return func <EOL> def color_gen ( n_colors , colors = None ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> if colors is None : <EOL> for color in palettes . hls_palette ( n_colors = n_colors ) : <EOL> yield rgb2hex ( color ) <EOL> else : <EOL> for color in colors : <EOL> yield color <EOL> def assign_colors ( data , aes , gg , aes_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> legend_entry = dict ( ) <EOL> if aes_name in aes : <EOL> color_col = aes [ aes_name ] <EOL> labels , scale_type , indices = get_labels ( data , color_col ) <EOL> if scale_type == "<STR_LIT>" : <EOL> data , legend_entry = assign_continuous_colors ( <EOL> data , gg , aes_name , color_col , labels , indices ) <EOL> elif scale_type == "<STR_LIT>" : <EOL> data , legend_entry = assign_discrete_colors ( <EOL> data , gg , aes_name , color_col , labels ) <EOL> else : <EOL> raise GgplotError ( "<STR_LIT>" % scale_type ) <EOL> return data , legend_entry <EOL> def assign_continuous_colors ( data , gg , aes_name , color_col , labels , indices ) : <EOL> """<STR_LIT>""" <EOL> _mcolumn = '<STR_LIT>' % aes_name <EOL> min_val = data [ color_col ] . min ( ) <EOL> max_val = data [ color_col ] . max ( ) <EOL> values = ( data [ color_col ] - min_val ) / ( max_val - min_val ) <EOL> values = values . tolist ( ) <EOL> color_mapping = gg . colormap ( values ) [ : : , : <NUM_LIT:3> ] <EOL> data [ _mcolumn ] = [ rgb2hex ( value ) for value in color_mapping ] <EOL> key_colors = gg . colormap ( indices ) [ : : , : <NUM_LIT:3> ] <EOL> key_colors = [ rgb2hex ( value ) for value in key_colors ] <EOL> legend_entry = { '<STR_LIT>' : color_col , <EOL> '<STR_LIT>' : dict ( zip ( key_colors , labels ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> return data , legend_entry <EOL> def assign_discrete_colors ( data , gg , aes_name , color_col , labels ) : <EOL> """<STR_LIT>""" <EOL> _mcolumn = '<STR_LIT>' % aes_name <EOL> possible_colors = np . unique ( data [ color_col ] ) <EOL> if gg . manual_color_list : <EOL> color = color_gen ( len ( possible_colors ) , gg . manual_color_list ) <EOL> else : <EOL> color = color_gen ( len ( possible_colors ) ) <EOL> color_mapping = dict ( ( value , six . next ( color ) ) for value in possible_colors ) <EOL> data [ _mcolumn ] = data [ color_col ] . apply ( lambda x : color_mapping [ x ] ) <EOL> legend_entry = { '<STR_LIT>' : color_col , <EOL> '<STR_LIT>' : dict ( ( v , k ) for k , v in color_mapping . items ( ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> return data , legend_entry </s>
