<s> from __future__ import division <EOL> import colorsys <EOL> from itertools import cycle <EOL> import numpy as np <EOL> import matplotlib as mpl <EOL> from six import string_types <EOL> from six . moves import range <EOL> from . import husl <EOL> from . xkcd_rgb import xkcd_rgb <EOL> def desaturate ( color , prop ) : <EOL> """<STR_LIT>""" <EOL> if not <NUM_LIT:0> <= prop <= <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> rgb = mplcol . colorConverter . to_rgb ( color ) <EOL> h , l , s = colorsys . rgb_to_hls ( * rgb ) <EOL> s *= prop <EOL> new_color = colorsys . hls_to_rgb ( h , l , s ) <EOL> return new_color <EOL> class _ColorPalette ( list ) : <EOL> """<STR_LIT>""" <EOL> def __enter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> from . rcmod import set_palette <EOL> self . _orig_palette = color_palette ( ) <EOL> set_palette ( self , len ( self ) ) <EOL> return self <EOL> def __exit__ ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> from . rcmod import set_palette <EOL> set_palette ( self . _orig_palette , len ( self . _orig_palette ) ) <EOL> def color_palette ( name = None , n_colors = <NUM_LIT:6> , desat = None ) : <EOL> """<STR_LIT>""" <EOL> seaborn_palettes = dict ( <EOL> deep = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> muted = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> pastel = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> bright = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> dark = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> colorblind = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ) <EOL> if name is None : <EOL> palette = mpl . rcParams [ "<STR_LIT>" ] <EOL> elif not isinstance ( name , string_types ) : <EOL> palette = name <EOL> elif name == "<STR_LIT>" : <EOL> palette = hls_palette ( n_colors ) <EOL> elif name == "<STR_LIT>" : <EOL> palette = husl_palette ( n_colors ) <EOL> elif name in seaborn_palettes : <EOL> palette = seaborn_palettes [ name ] <EOL> elif name in dir ( mpl . cm ) : <EOL> palette = mpl_palette ( name , n_colors ) <EOL> elif name [ : - <NUM_LIT:2> ] in dir ( mpl . cm ) : <EOL> palette = mpl_palette ( name , n_colors ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % name ) <EOL> if desat is not None : <EOL> palette = [ desaturate ( c , desat ) for c in palette ] <EOL> pal_cycle = cycle ( palette ) <EOL> palette = [ next ( pal_cycle ) for _ in range ( n_colors ) ] <EOL> try : <EOL> palette = map ( mpl . colors . colorConverter . to_rgb , palette ) <EOL> palette = _ColorPalette ( palette ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" % str ( name ) ) <EOL> return palette <EOL> def hls_palette ( n_colors = <NUM_LIT:6> , h = <NUM_LIT> , l = <NUM_LIT> , s = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> hues = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , n_colors + <NUM_LIT:1> ) [ : - <NUM_LIT:1> ] <EOL> hues += h <EOL> hues %= <NUM_LIT:1> <EOL> hues -= hues . astype ( int ) <EOL> palette = [ colorsys . hls_to_rgb ( h_i , l , s ) for h_i in hues ] <EOL> return palette <EOL> def husl_palette ( n_colors = <NUM_LIT:6> , h = <NUM_LIT> , s = <NUM_LIT> , l = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> hues = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , n_colors + <NUM_LIT:1> ) [ : - <NUM_LIT:1> ] <EOL> hues += h <EOL> hues %= <NUM_LIT:1> <EOL> hues *= <NUM_LIT> <EOL> s *= <NUM_LIT> <EOL> l *= <NUM_LIT> <EOL> palette = [ husl . husl_to_rgb ( h_i , s , l ) for h_i in hues ] <EOL> return palette <EOL> def mpl_palette ( name , n_colors = <NUM_LIT:6> ) : <EOL> """<STR_LIT>""" <EOL> brewer_qual_pals = { "<STR_LIT>" : <NUM_LIT:8> , "<STR_LIT>" : <NUM_LIT:8> , "<STR_LIT>" : <NUM_LIT:12> , <EOL> "<STR_LIT>" : <NUM_LIT:9> , "<STR_LIT>" : <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:9> , "<STR_LIT>" : <NUM_LIT:8> , "<STR_LIT>" : <NUM_LIT:12> } <EOL> if name . endswith ( "<STR_LIT>" ) : <EOL> pal = [ "<STR_LIT>" ] <EOL> pal . extend ( color_palette ( name . replace ( "<STR_LIT>" , "<STR_LIT>" ) , <NUM_LIT:2> ) ) <EOL> cmap = blend_palette ( pal , n_colors , as_cmap = True ) <EOL> else : <EOL> cmap = getattr ( mpl . cm , name ) <EOL> if name in brewer_qual_pals : <EOL> bins = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , brewer_qual_pals [ name ] ) [ : n_colors ] <EOL> else : <EOL> bins = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , n_colors + <NUM_LIT:2> ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> palette = list ( map ( tuple , cmap ( bins ) [ : , : <NUM_LIT:3> ] ) ) <EOL> return palette <EOL> def dark_palette ( color , n_colors = <NUM_LIT:6> , reverse = False , as_cmap = False ) : <EOL> """<STR_LIT>""" <EOL> gray = "<STR_LIT>" <EOL> colors = [ color , gray ] if reverse else [ gray , color ] <EOL> return blend_palette ( colors , n_colors , as_cmap ) <EOL> def blend_palette ( colors , n_colors = <NUM_LIT:6> , as_cmap = False ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT:->" . join ( map ( str , colors ) ) <EOL> pal = mpl . colors . LinearSegmentedColormap . from_list ( name , colors ) <EOL> if not as_cmap : <EOL> pal = pal ( np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , n_colors ) ) <EOL> return pal <EOL> def xkcd_palette ( colors ) : <EOL> """<STR_LIT>""" <EOL> palette = [ xkcd_rgb [ name ] for name in colors ] <EOL> return color_palette ( palette , len ( palette ) ) <EOL> def cubehelix_palette ( n_colors = <NUM_LIT:6> , start = <NUM_LIT:0> , rot = <NUM_LIT> , gamma = <NUM_LIT:1.0> , hue = <NUM_LIT> , <EOL> light = <NUM_LIT> , dark = <NUM_LIT> , reverse = False , as_cmap = False ) : <EOL> """<STR_LIT>""" <EOL> cdict = mpl . _cm . cubehelix ( gamma , start , rot , hue ) <EOL> cmap = mpl . colors . LinearSegmentedColormap ( "<STR_LIT>" , cdict ) <EOL> x = np . linspace ( light , dark , n_colors ) <EOL> pal = cmap ( x ) [ : , : <NUM_LIT:3> ] . tolist ( ) <EOL> if reverse : <EOL> pal = pal [ : : - <NUM_LIT:1> ] <EOL> if as_cmap : <EOL> x_256 = np . linspace ( light , dark , <NUM_LIT> ) <EOL> if reverse : <EOL> x_256 = x_256 [ : : - <NUM_LIT:1> ] <EOL> pal_256 = cmap ( x_256 ) <EOL> cmap = mpl . colors . ListedColormap ( pal_256 ) <EOL> return cmap <EOL> else : <EOL> return pal </s>
