#197 Available color palettes with Matplotlib

The post #196 describes how to pick up a single color when working with python and matplotlib. This post aims to describe a few color palette that are provided, and thus make your life easier when plotting several color. 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!

    # library & dataset
    from matplotlib import pyplot as plt
    import numpy as np
    # create data
    x = np.random.rand(15)
    y = x+np.random.rand(15)
    z = x+np.random.rand(15)
    # Use it with a call in cmap
    plt.scatter(x, y, s=z*2000, c=x, cmap="BuPu", alpha=0.4, edgecolors="grey", linewidth=2)
    # You can reverse it:
    plt.scatter(x, y, s=z*2000, c=x, cmap="BuPu_r", alpha=0.4, edgecolors="grey", linewidth=2)
    # OTHER: viridis / inferno / plasma / magma
    plt.scatter(x, y, s=z*2000, c=x, cmap="plasma", alpha=0.4, edgecolors="grey", linewidth=2)
  • 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:

    # library & dataset
    from matplotlib import pyplot as plt
    import numpy as np
    # create data
    x = np.random.rand(80) - 0.5
    y = x+np.random.rand(80)
    z = x+np.random.rand(80)
    # plot
    plt.scatter(x, y, s=z*2000, c=x, cmap="PuOr", alpha=0.4, edgecolors="grey", linewidth=2)
    # reverse. Just load the seaborn library for a nice looking appearance.
    import seaborn as sns
    plt.scatter(x, y, s=z*2000, c=x, cmap="PuOr_r", alpha=0.4, edgecolors="grey", linewidth=2)



    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!









    # library & dataset
    from matplotlib import pyplot as plt
    import numpy as np
    # Data
    import seaborn as sns
    df = sns.load_dataset('iris')
    # We use the specie column to choose the color. We need to make a numerical vector from it:
    # Scatter
    plt.scatter(df['sepal_length'], df['sepal_width'], s=62, c=df['species'].cat.codes, cmap="Set1", alpha=0.9, linewidth=0)
  • Sponsors

  • Leave a Reply

    Your email address will not be published.