[PYTHON] How to choose a Seaborn color palette

Introduction

This is a compilation of Choosing color palettes of the python drawing package seaborn. A Jupyter file is also available.

Preparation

python


%matplotlib inline
import seaborn as sns, numpy as np
from ipywidgets import interact, FloatSlider

How to create using color_palette ()
(Building color palettes with color_palette ())

--color_palette can be used to create most color palettes. You can use set_palette to set the default color palette (see below for an example).

Qualitative color palettes
(Qualitative color palettes)

--If you do not specify the color palette name, you can get the current color palette. (The following is the default color palette) --palplot displays the color palette.

python


current_palette = sns.color_palette(n_colors=24)
sns.palplot(current_palette)

image

The default color palette has 6 themes (deep, muted, pastel, bright, dark, color blind). (Default is deep) In Jupyter, you can check the theme interactively.

python


def show_pal0(palette):
    sns.palplot(sns.color_palette(palette, 24))
interact(show_pal0, palette='deep muted pastel bright dark colorblind'.split());

image

Hue color palette
(Using circular color systems)

Frequently used is hls. You can create it with either color_palette or hls_palette.

python


sns.palplot(sns.color_palette("hls",24))
sns.palplot(sns.hls_palette(24))

image image

You can specify the brightness with the parameter l and the saturation with s.

python


sns.palplot(sns.hls_palette(24, l=0.2))
sns.palplot(sns.hls_palette(24, s=0.2))

image image

You can get a clear color palette by doing the following.

python


sns.palplot(sns.hls_palette(24, l=0.5, s=1))

image

In Jupyter, you can check the brightness and saturation interactively.

python


def show_pal1(l, s):
    sns.palplot(sns.hls_palette(24, l=l, s=s))
interact(show_pal1, l=FloatSlider(0.6, max=1), s=FloatSlider(0.65, max=1));

image

Husl is also available, which reduces the variation in brightness between hues of hls.

python


sns.palplot(sns.husl_palette(24))

image

Category Color
(Using categorical Color Brewer palettes)

A color palette with a name. It's a bit old, but I've prepared a list (PDF for printing). image

Reference: https://matplotlib.org/examples/color/colormaps_reference.html

python


sns.palplot(sns.color_palette("Set1", 24))

image

It's easy to see in Jupyter.

python


sns.choose_colorbrewer_palette('qualitative');

image

You can also specify in RGB to create a color palette.

python


flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
sns.palplot(sns.color_palette(flatui, 24))

image

Continuous color palettes
(Sequential color palettes)

--Specify a specific name for a continuous color palette. --If you add "_d" to the name, it will be dark. (Light if not attached) --If you add "_r" to the name, the order will be reversed.

python


sns.palplot(sns.color_palette("Blues", 24))
sns.palplot(sns.color_palette("Blues_d", 24))
sns.palplot(sns.color_palette("Blues_r", 24))

image image image

It's easy to see in Jupyter.

python


sns.choose_colorbrewer_palette('sequential');

image

Continuous color palettes with cubehelix_palette
(Sequential palettes with cubehelix_palette ())

You can use cubehelix to create a color palette with continuously changing brightness while changing color tones.

python


sns.palplot(sns.color_palette("cubehelix", 24))

image

cubehelix_palette seems to be a different color palette.

python


sns.palplot(sns.cubehelix_palette(24))

image

By setting as_cmap = True, it can be used in graph drawing with cmap parameter.

python


np.random.seed(1)
x, y = np.random.multivariate_normal([0, 0], [[1, -.5], [-.5, 1]], size=300).T
cmap = sns.cubehelix_palette(light=1, as_cmap=True)
sns.kdeplot(x, y, cmap=cmap, shade=True);

image

In Jupyter, you can check the cubehelix_palette color palette interactively.

python


def show_pal2(start, rot):
    sns.palplot(sns.cubehelix_palette(24, start=start, rot=rot))
interact(show_pal2, start=FloatSlider(max=1), rot=FloatSlider(0.4, max=1));

image

Custom sequential palettes with light_palette () and dark_palette ())

You can also use light_palette and dark_palette.

python


sns.palplot(sns.light_palette("blue", 24))
sns.palplot(sns.dark_palette("blue", 24))

image image

Let's use it for the contour lines in the previous figure.

python


cmap = sns.dark_palette("palegreen", as_cmap=True)
sns.kdeplot(x, y, cmap=cmap);

image

Enabled to check interactively with Jupyter.

python


def show_pal3(light_or_dark, color, reverse):
    sns.palplot(eval('sns.%s_palette'%light_or_dark)(color=color, n_colors=24, reverse=reverse))
interact(show_pal3, light_or_dark=('light', 'dark'), color=('blue', 'navy', 'green', 'palegreen', 'red'), reverse=False);

image

Color palettes divided into two colors
(Diverging color palettes)

A color palette with different colors on both ends and white in the middle. You can create it by specifying it with color_palette.

python


sns.palplot(sns.color_palette("BrBG", 24))
sns.palplot(sns.color_palette("RdBu_r", 24))
sns.palplot(sns.color_palette("coolwarm", 24))

image image image

It's easy to see in Jupyter.

python


sns.choose_colorbrewer_palette('diverging');

image

Custom color palettes divided into two colors
(Custom diverging palettes with diverging_palette ())

You can customize it with diverging_palette. You can also darken the middle.

python


sns.palplot(sns.diverging_palette(220, 20, n=24))
sns.palplot(sns.diverging_palette(145, 280, s=85, l=25, n=24))
sns.palplot(sns.diverging_palette(255, 133, l=60, n=24, center="dark"))

image image image

Enabled to check interactively with Jupyter.

python


def show_pal4(h_neg, h_pos, s, l, center):
    sns.palplot(sns.diverging_palette(h_neg, h_pos, n=24, s=s, l=l, center=center))
interact(show_pal4, h_neg=FloatSlider(220, max=360), h_pos=FloatSlider(20, max=360), 
         s=FloatSlider(75, max=99), l=FloatSlider(50, max=99), center=('light', 'dark'));

image

Setting default palettes
(Changing default palettes with set_palette ())

You can set the default color palette with set_palette. Draws without settings.

python


def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

sinplot()

image

Set and draw.

python


sns.set_palette("husl")
sinplot()

image

You can change it locally using the with clause.

python


with sns.color_palette("PuBuGn_d"):
    sinplot()

image

that's all

Recommended Posts