Share @ LinkedIn Facebook  datascience, datavisulisation, seaborn
Seaborn Tutorial - Color Pallete

Seaborn Color Palette Tutorial

In this tutorial, we will learn about Color plays an important role than any other aspect of the visualizations. When used effectively, color adds more value to the plot. A palette means a flat surface on which a painter arranges and mixes paints.

Topic #1 How to build a Color Palette

Seaborn provides a function called color_palette(), which can be used to give colors to plots and adding more aesthetic value to it. Also, Seaborn Color Palette makes the visual appearance of your work 4 times better, gives a better picture and easy to understand.

Here we provide a syntacial way of reprentation ----
seaborn.color_palette(palette = None, n_colors = None, desat = None)

Return

Return refers to the list of RGB tuples. Following are the readily available Seaborn palettes −

  • Deep
  • Muted
  • Bright
  • Pastel
  • Dark
  • Colorblind

Besides these, one can also generate new palette

It is hard to decide which palette should be used for a given data set without knowing the characteristics of data. Being aware of it, we will classify the different ways of using color_palette() types −

  • Qualitative
  • Sequential
  • Diverging

We have another function seaborn.palplot() which deals with color palettes. This function plots the color palette as a horizontal array. We will know more regarding seaborn.palplot() in the coming examples.

Qualitative Color Palettes

Qualitative or categorical palettes are best suitable to plot the categorical data.

In [1]:
from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(current_palette)
plt.show()

We know that We haven’t passed any parameters in color_palette(); by default, we are seeing 6 colors. You can see the desired number of colors by passing a value to the n_colors parameter. Here, the palplot() is used to plot the array of colors horizontally.

Sequential Color Palettes

Sequential plots are suitable to express the distribution of data ranging from relative lower values to higher values within a range.

Appending an additional character ‘s’ to the color passed to the color parameter will plot the Sequential plot.

In [5]:
from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("Greens"))
plt.show()
In [6]:
from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("Reds"))
plt.show()
In [7]:
from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("Blues"))
plt.show()
In [1]:
from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("Purples"))
plt.show()

Diverging Color Palette

Diverging palettes use two different colors. Each color represents variation in the value ranging from a common point in either direction.

Assume plotting the data ranging from -1 to 1. The values from -1 to 0 take one color and 0 to +1 take another color.

By default, the values are centered from zero. You can control it with the parameter center by passing a value.

In [3]:
from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("BrBG", 7))
plt.show()

Setting the Default Color Palette

The functions color_palette() has a companion called set_palette(). The relationship between them is similar to the pairs covered in the aesthetics chapter. The arguments are the same for both set_palette() and color_palette(), but the default Matplotlib parameters are changed so that the palette is used for all plots.

In [5]:
import numpy as np
from matplotlib import pyplot as plt
def sinplot(flip = 1):
   x = np.linspace(0, 14, 100)
   for i in range(1, 5):
      plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

import seaborn as sb
sb.set_style("white")
sb.set_palette("muted")
sinplot()
plt.show()


Dolly Solanki  Dolly Solanki