Updated On : Jan-28,2020 Time Investment : ~13 mins

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.

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.

from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("Greens"))
plt.show()
from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("Reds"))
plt.show()
from matplotlib import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("Blues"))
plt.show()
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.

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.

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

Share Views Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.