<s> from __future__ import division <EOL> from future . builtins import zip <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from matplotlib . colors import LinearSegmentedColormap <EOL> from . _config import config <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class LinearColormap ( LinearSegmentedColormap ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , color_data , index = None , ** kwargs ) : <EOL> if not hasattr ( color_data , '<STR_LIT>' ) : <EOL> color_data = rgb_list_to_colordict ( color_data ) <EOL> if index is None : <EOL> index = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , len ( color_data [ '<STR_LIT>' ] ) ) <EOL> color_data = dict ( ( key , [ ( x , y , y ) for x , y in zip ( index , value ) ] ) <EOL> for key , value in color_data . items ( ) ) <EOL> LinearSegmentedColormap . __init__ ( self , name , color_data , ** kwargs ) <EOL> def rgb_list_to_colordict ( rgb_list ) : <EOL> colors_by_channel = list ( zip ( * rgb_list ) ) <EOL> channels = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return dict ( ( color , value ) <EOL> for color , value in zip ( channels , colors_by_channel ) ) <EOL> CMAP_RANGE = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def color_mapper ( parameter_range , cmap = None , start = None , stop = None ) : <EOL> """<STR_LIT>""" <EOL> if cmap is None : <EOL> cmap = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if isinstance ( cmap , str ) : <EOL> cmap = getattr ( plt . cm , cmap ) <EOL> crange = list ( CMAP_RANGE . get ( cmap . name , ( <NUM_LIT:0> , <NUM_LIT:1> ) ) ) <EOL> if start is None : <EOL> start = crange [ <NUM_LIT:0> ] <EOL> if stop is None : <EOL> stop = crange [ <NUM_LIT:1> ] <EOL> assert <NUM_LIT:0> <= start <= <NUM_LIT:1> <EOL> assert <NUM_LIT:0> <= stop <= <NUM_LIT:1> <EOL> pmin , pmax = parameter_range <EOL> def map_color ( val ) : <EOL> """<STR_LIT>""" <EOL> assert pmin <= val <= pmax <EOL> val_norm = ( val - pmin ) * float ( stop - start ) / ( pmax - pmin ) <EOL> idx = val_norm + start <EOL> return cmap ( idx ) <EOL> return map_color <EOL> def colors_from_cmap ( length = <NUM_LIT:50> , cmap = None , start = None , stop = None ) : <EOL> """<STR_LIT>""" <EOL> if cmap is None : <EOL> cmap = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if isinstance ( cmap , str ) : <EOL> cmap = getattr ( plt . cm , cmap ) <EOL> crange = list ( CMAP_RANGE . get ( cmap . name , ( <NUM_LIT:0> , <NUM_LIT:1> ) ) ) <EOL> if start is not None : <EOL> crange [ <NUM_LIT:0> ] = start <EOL> if stop is not None : <EOL> crange [ <NUM_LIT:1> ] = stop <EOL> assert <NUM_LIT:0> <= crange [ <NUM_LIT:0> ] <= <NUM_LIT:1> <EOL> assert <NUM_LIT:0> <= crange [ <NUM_LIT:1> ] <= <NUM_LIT:1> <EOL> idx = np . linspace ( crange [ <NUM_LIT:0> ] , crange [ <NUM_LIT:1> ] , num = length ) <EOL> return cmap ( idx ) <EOL> def cycle_cmap ( length = <NUM_LIT:50> , cmap = None , start = None , stop = None , ax = None ) : <EOL> """<STR_LIT>""" <EOL> color_cycle = colors_from_cmap ( length , cmap , start , stop ) <EOL> if ax is None : <EOL> plt . rc ( '<STR_LIT>' , color_cycle = color_cycle . tolist ( ) ) <EOL> else : <EOL> ax . set_color_cycle ( color_cycle ) </s>
