We can use the create_dendrogram() function from the Plotly.figure_factory to create a dendrogram plot.
Plotly.figure_factory.create_dendrogram()
The function provides a syntax as shown in the following:
The parameter functionality is as follows:
- x – Specifies the matrix of observations as an array of arrays.
- orientation – Sets the orientation of the plot. Accepted values include:
- ‘top’
- ‘right’
- ‘bottom’
- ‘left’
- colorscale – Sets a colorscale for the dendrogram figure.
- distfun – Represents the function used to calculate the pairwise distance from the observations.
- linkagefun – Sets the function to determine the linkage matrix from the pairwise distance.
- hovertext – Sets the hover text for the traces of the dendrogram cluster.
- color_threshold – Defines the value used to make the separation of the clusters.
Example 1:
The following code shows how to create a simple dendrogram tree with the orientation set to bottom.
import numpy as np
x = np.random.rand(10,10)
fig = ff.create_dendrogram(x, orientation='bottom')
fig.show()
The previous code returns a dendrogram plot as in the following figure:
You can change the orientation to any value that suits your needs.
Example 2:
You can also set the labels for the dendrogram by specifying the labels parameter.
chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
fig = ff.create_dendrogram(x, orientation='left', labels=chars)
fig.show()
Output:
Conclusion:
This article covers the basics of creating a dendrogram figure in Plotly.