SciPy - set_link_color_palette(s) Method



The SciPy set_link_color_palette() Method is used to perform the operation of matplotlib color codes. It allows user to set the customize colors while representing the different clusters in a dendrogram. This method is part of the scipy.cluster.hierarchy module.

Following are the usage of this method used in data analysis −

  • Hierarchical Clustering Visualization: This shows data of different colors for different clusters.
  • Data Presentation: Data are more readable and visual appealing for the representation.
  • Pattern Recognition: This helps us for the identification of clusters and relationship with a larger datasets.

Syntax

Following is the syntax of the SciPy set_link_color_palette() Method −

set_link_color_palette(['color_code_1', 'color_code_2', ...])

Parameters

This method accepts the custom color palette based on data inputs.

Return value

This method doesn't return any type.

Example 1

Following is the SciPy set_link_color_palette() Method that illustrates the different color palette within a given input data.

import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette

# given data
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])

# hierarchical/agglomerative clustering
res = linkage(X, 'ward')

# set the custom color palette
set_link_color_palette(['r', 'g', 'b', 'c', 'm', 'y'])

# Plot dendrogram
dendrogram(res)
plt.show()

Output

The above code produces the following output −

set_link_color_palette_one

Example 2

Here, we are using random.rand() to set the given data and shows color palette with the help of hexadecimal color codes(eg. #33FF57).

import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette

# given data
X = np.random.rand(10, 2)

# hierarchical/agglomerative clustering
res = linkage(X, 'single')

# set a custom color palette using hexadecimal color codes
set_link_color_palette(['#FF5733', '#33FF57', '#3357FF', '#FF33A1'])

# plot dendrogram
dendrogram(res)
plt.show()

Output

The above code produces the following output −

set_link_color_palette_two

Example 3

Below the program demonstrates the color palette for a larger dataset using set_link_color_palette().

import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette

# given data
X = np.random.rand(50, 2)

# hierarchical/agglomerative clustering
res = linkage(X, 'complete')

# set a larger custom color palette
palette = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF',
           '#800000', '#808000', '#008000', '#800080', '#008080', '#000080']
set_link_color_palette(palette)

# plot dendrogram
dendrogram(res)
plt.show()

Output

The above code produces the following output −

set_link_color_palette_three
scipy_reference.htm
Advertisements