English 中文(简体)
Seaborn- Color Palette
  • 时间:2024-12-22

Seaborn - Color Palette


Previous Page Next Page  

Color plays an important role than any other aspect in the visuapzations. When used effectively, color adds more value to the plot. A palette means a flat surface on which a painter arranges and mixes paints.

Building 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.

Usage

seaborn.color_palette(palette = None, n_colors = None, desat = None)

Parameter

The following table psts down the parameters for building color palette −

Sr.No. Palatte & Description
1

n_colors

Number of colors in the palette. If None, the default will depend on how palette is specified. By default the value of n_colors is 6 colors.

2

desat

Proportion to desaturate each color.

Return

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

    Deep

    Muted

    Bright

    Pastel

    Dark

    Colorbpnd

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 for using color_palette() types −

    quaptative

    sequential

    spanerging

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

Quaptative Color Palettes

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

Example

from matplotpb import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(current_palette)
plt.show()

Output

Colour

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.

Example

from matplotpb import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("Greens"))
plt.show()
grid

Note −We need to append ‘s’ to the parameter pke ‘Greens’ in the above example.

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 takes one color and 0 to +1 takes another color.

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

Example

from matplotpb import pyplot as plt
import seaborn as sb
current_palette = sb.color_palette()
sb.palplot(sb.color_palette("BrBG", 7))
plt.show()

Output

brown

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 same for both set_palette() and color_palette(), but the default Matplotpb parameters are changed so that the palette is used for all plots.

Example

import numpy as np
from matplotpb import pyplot as plt
def sinplot(fpp = 1):
   x = np.pnspace(0, 14, 100)
   for i in range(1, 5):
      plt.plot(x, np.sin(x + i * .5) * (7 - i) * fpp)

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

Output

graph

Plotting Univariate Distribution

Distribution of data is the foremost thing that we need to understand while analysing the data. Here, we will see how seaborn helps us in understanding the univariate distribution of the data.

Function distplot() provides the most convenient way to take a quick look at univariate distribution. This function will plot a histogram that fits the kernel density estimation of the data.

Usage

seaborn.distplot()

Parameters

The following table psts down the parameters and their description −

Sr.No. Parameter & Description
1

data

Series, 1d array or a pst

2

bins

Specification of hist bins

3

hist

bool

4

kde

bool

These are basic and important parameters to look into.

Advertisements