jmquintana79
10/4/2017 - 10:15 AM

build palette by value

Build palette by value using Seaborn (SNS) library. Bigger values of y has got stronger colours.


## build association between y values and palette
def palete(xr,yr,scolor='blue'):
    import numpy as np
    import pandas as pd
    import seaborn as sns
    # define original palette
    #lpalette = sns.light_palette((210, 90, 60), input="husl",n_colors=len(xr))
    lpalette = sns.light_palette(scolor,n_colors=len(xr))
    # build association between y values and palette
    BAR = pd.DataFrame(np.array(list(zip(list(range(len(xr))),xr,yr))))
    BAR.columns = ['ix','x','y']
    BAR.y = BAR.y.astype(float) 
    BAR.sort_values(['y'], ascending=[1], inplace=True)
    BAR['ipalette'] = list(range(len(x)))
    BAR.sort_values(['ix'], ascending=[1], inplace=True)
    # return
    return [lpalette[i] for i in BAR.ipalette.values]
    
  
if __name__ == __main__:
  import seaborn as sns
  sns.set(style="ticks")
  sns.boxplot(x="colx", y="coly", data=df, palette=palete(xr,yr,'yellow'))
  sns.despine(offset=10, trim=True)