Author avatar

Vivek Kumar

Choosing Color Palettes

Vivek Kumar

  • Apr 19, 2019
  • 11 Min read
  • 492 Views
  • Apr 19, 2019
  • 11 Min read
  • 492 Views
Data
Seaborn

Introduction

In this guide, you are going to learn about the fundamentals of building and selecting a built-in color palette in a Seaborn figure. The Matplotlib package in python also provides a range of building and selecting color palette as discussed in my guide Customizing Colormaps.

By the end of this guide you will be able to implement the following concepts:

  1. Building a discrete color palette
  2. Selecting from a built-in color palette

The Baseline

In this guide we are going to use the following libraries:

Syntax

1
2
3
4
5
# Importing necessary libraries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
python

Building a Discrete Color Palette

A color palette in Seaborn can be generated using the color_palette method which accepts color codes in XKCD RGB, or hex format. It can also be used to generate colormaps using built-in Seaborn color palettes and Matplotlib colormaps (except Jet).

In this section, let us learn how to build a color palette using XKCD RGB, and hex codes.

Note - To learn more about color theory, you can refer to this article. Also, the information provided by the image given below is also valuable while modifying a color palette.

Seaborn

1. Building Color Palette using XKCD RGB

The XKCD consists of 954 most common RGB colors with specific names. You can find the complete list here. So, there is no need to remember the codes, as you can call a color using its readable name like hot pink, sea green, periwinkle, forest green, mustard, etc. Let us implement these eight colors on a barplot using the sns.xkcd_palette method as shown:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Setting the first figure size
plt.figure(0, figsize=(8, 5))

# Initializing the data
x = ['sky', 					# Hex code: #82cafc
		'rose', 				 # Hex code: #cf6275
		'forest green', 	#  Hex code: #06470c
		'burgundy', 		#  Hex code: #610023
		'khaki', 				# Hex code: #aaa662
		'chartreuse', 		# Hex code: #c1f80a
		'goldenrod', 		# Hex code: #fac205
		'pumpkin']			# Hex code: #e17701
y = np.array([50, 44, 29, 69, 82, 35, 37, 50])
df = pd.DataFrame({'x': x, 'y': y})

# Setting ticks figure style
sns.set_style('ticks')

# Plotting
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.xkcd_palette(x))
sns.despine()

# Labelling
plt.title('XKCD RGB Colors', weight='bold', fontsize=18)

# Displaying the plot
plt.show()
python

XKCD

2. Building a Color Palette Using Hex Codes

You have now observed that you can call 954 hex codes using XKCD RGB format, however, if you still want to pass a list of hex color code values, then you can directly pass the list inside the color_palette method as shown here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Setting the first figure size
plt.figure(0, figsize=(8, 5))

# Initializing the data
x = ['tangerine', 'denim', 'lemon', 'avocado', 
     'fluorescent green', 'barbie pink', 'shit', 'mocha']
y = np.array([50, 44, 29, 69, 82, 35, 37, 50])
df = pd.DataFrame({'x': x, 'y': y})

# Setting ticks figure style
sns.set_style('ticks')

# Plotting
sns.barplot(x='y', y='x', data=df, orient='h', 
            palette=sns.color_palette(['#ff9408', '#3b638c', '#fdff52', '#90b134',
													'#08ff08', '#fe46a5', '#7f5f00', '#9d7651']))
sns.despine()

# Labelling
plt.title('Hex Color Codes', weight='bold', fontsize=18)

# Displaying the plot
plt.show()
python

HEX

Selecting From a Built-in Color Palette

There are various built-in color palettes available in Seaborn as listed:

1
Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r

As you can observe, all the color names have a reverse color name with suffix _r. This means that if a color, say C, has a list of color values [p, q, r, s], then C_r will have an order [s, r, q, p].

However, as discussed in the Matplotlib colormap guide, choosing a color is based on a scenario and, accordingly, four color classes can be described which are:

  1. Qualitative
  2. Sequential
  3. Diverging
  4. Miscellaneous

Let us learn to build a color palette for the first three classes. To learn about what and where to use these classes, refer the matplotlib colormap guide mentioned above.

1. Qualitative

To create a qualitative color palette, you need to choose discrete colors with no order. You can create such palettes using methods discussed in the previous section. Let us use the knowledge of luminance and saturation gained from the figure presented at the top of the guide and implement a qualitative color palette:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Setting the figure
fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(2, 2, figsize=(14, 10))

# Initializing the data
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
y = np.array([50, 44, 29, 69, 82, 35, 37, 50])
df = pd.DataFrame({'x': x, 'y': y})

# Setting ticks figure style
sns.set_style('ticks')

# Plotting subplot 1
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.hls_palette(8, l=.9, s=.1), ax=ax0)
sns.despine()
ax0.set_title('High Luminance and Low Saturation', weight='bold', fontsize=18)

# Plotting subplot 2
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.hls_palette(8, l=.9, s=.9), ax=ax1)
sns.despine()
ax1.set_title('High Luminance and High Saturation', weight='bold', fontsize=18)

# Plotting subplot 3
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.hls_palette(8, l=.1, s=.1), ax=ax2)
sns.despine()
ax2.set_title('Low Luminance and Low Saturation', weight='bold', fontsize=18)

# Plotting subplot 4
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.hls_palette(8, l=.1, s=.9), ax=ax3)
sns.despine()
ax3.set_title('Low Luminance and High Saturation', weight='bold', fontsize=18)

# Displaying the plot
plt.show()
python

Lumi-Sat

2. Sequential

The sequential color palette represents either an increasing or decreasing order. Most of the color palettes like Blues, Greens, Blues_r, etc. exist with sequential colors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Setting the figure
plt.figure(figsize=(8, 5))

# Initializing the data
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
y = np.array([50, 44, 29, 69, 82, 35, 37, 50])
df = pd.DataFrame({'x': x, 'y': y})

# Setting ticks figure style
sns.set_style('ticks')

# Plotting
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.color_palette('Blues'))
sns.despine()
plt.title('Blues Color Palette', weight='bold', fontsize=18)

# Displaying the plot
plt.show()
python

Blues

As you can observe, after F, the Blues palette has started to repeat its values (G and H).

Alternatively, cubehelix color palettes can also be used which can be helpful to visualize even by a colorblind people.

1
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.cubehelix_palette())
python

Cubehelix

Also, Seaborn provides light and dark color sequential palettes using sns.light_palette and sns.dark_palette methods which accept a palette name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Setting the figure
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 5))

# Initializing the data
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
y = np.array([50, 44, 29, 69, 82, 35, 37, 50])
df = pd.DataFrame({'x': x, 'y': y})

# Setting ticks figure style
sns.set_style('ticks')

# Plotting light color palette
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.light_palette('green'), ax=ax0)
sns.despine()
ax0.set_title('Light Color Palette', weight='bold', fontsize=18)

# Plotting dark color palette
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.dark_palette('green'), ax=ax1)
sns.despine()
ax1.set_title('Dark Color Palette', weight='bold', fontsize=18)

# Displaying the plot
plt.show()
python

Light-Dark

3. Diverging

To create diverging color palettes, Seaborn provides the sns.diverging_palette method which accepts anchor hues for negative and positive extents of the map, saturation, and many other parameters as shown with an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Setting the figure
plt.figure(figsize=(8, 5))

# Initializing the data
x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
y = np.array([50, 44, 29, 69, 82, 35, 37, 50])
df = pd.DataFrame({'x': x, 'y': y})

# Setting ticks figure style
sns.set_style('ticks')

# Plotting diverging color palette
sns.barplot(x='y', y='x', data=df, orient='h', palette=sns.diverging_palette(64, 256, s=50, l=50, n=8))
sns.despine()
plt.title('Diverging Color Palette', weight='bold', fontsize=18)

# Displaying the plot
plt.show()
python

Diverging

Conclusion

In this guide, you have learned to create your own color palettes and choose among various built-in color palettes available in Seaborn.

To learn more about Seaborn, you can refer the following guides:

0