#101 Make a color palette with Seaborn

The post #196 describes how to pick up a single color with matplotlib or seaborn. This post aims to describe a few color palette that are provided, and to explain how to call them in a Seaborn plot. There are 3 types of color palettes: Sequential, Discrete and Diverging. Here are a few explanations for each:

  •  

    A sequential color palette allows to describe a graduation. It goes from one bright colot to its dark form, from white to purple for example. In this case, the higher the value of X is, the darker is the colour. Find below a list of sequential palette. Note that you can easily reverse the palette just adding ‘_r‘ at the end of its name!

    # Libraries
    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # create data
    x = np.random.rand(80) - 0.5
    y = x+np.random.rand(80)
    z = x+np.random.rand(80)
    df = pd.DataFrame({'x':x, 'y':y, 'z':z})
    
    # Plot with palette
    sns.lmplot( x='x', y='y', data=df, fit_reg=False, hue='x', legend=False, palette="Blues")
    
    # reverse palette
    sns.lmplot( x='x', y='y', data=df, fit_reg=False, hue='x', legend=False, palette="Blues_r")
    
  • A diverging color palette is slightly different from a sequential color palette, even if it is used to show a graduation as well. It uses a first color graduation from the minimum to a critical midpoint (orange until 0 in our example), and then use another color to go to the maximum (purple in our example). Well a picture speaks better than thousand of words:

    # Libraries
    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    
    # create data
    x = np.random.rand(80) - 0.5
    y = x+np.random.rand(80)
    z = x+np.random.rand(80)
    df = pd.DataFrame({'x':x, 'y':y, 'z':z})
    
    # plot
    sns.lmplot( x='x', y='y', data=df, fit_reg=False, hue='x', legend=False, palette="PuOr")
    
    # reverse palette
    sns.lmplot( x='x', y='y', data=df, fit_reg=False, hue='x', legend=False, palette="PuOr_r")
    
  • A discrete color palette is used to represent, well, a discrete or categorical variable! For example, if you have 3 groups in the same scatterplot, you probably want to represent them with different colors. Using a palette helps you with your choice: it provides colors that go well together, that are distincts, and color blind friendly! Note that you can build this palette color by color as well (second chart)

     

    # library & dataset
    import seaborn as sns
    df = sns.load_dataset('iris')
    
    # --- Use the 'palette' argument of seaborn
    sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False, palette="Set1")
    plt.legend(loc='lower right')
    
    # --- Use a handmade palette
    flatui = ["#9b59b6", "#3498db", "orange"]
    sns.set_palette(flatui)
    sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False)
    
    

Leave a Reply

Your email address will not be published.