.create_dendrogram()
The .create_dendrogram()
function in Plotly’s figure_factory
module generates hierarchical cluster trees (dendrograms) for visualizing the arrangement of data clusters. It is useful in hierarchical clustering applications, such as bioinformatics and machine learning, to depict relationships between data points.
Syntax
import plotly.figure_factory as ff
dendrogram = ff.create_dendrogram(
X,
orientation="bottom",
labels=None,
colorscale="Viridis",
distfun=None,
linkagefun=None,
hovertext=None
)
Parameters:
X
: A matrix-like 2D list or NumPy array containing the data to be clustered.orientation
(default=”bottom”): Specifies the orientation of the dendrogram ('top'
,'bottom'
,'left'
, or'right'
).labels
(optional): A list of labels for the data points. IfNone
, default labels(0, 1, 2,...)
are used.colorscale
(default=”Viridis”): Determines the color scale used for branch coloring.distfun
(default=scipy.spatial.distance.pdist
): A function to compute pairwise distances between data points.linkagefun
(default=scipy.cluster.hierarchy.linkage
): A function to compute the hierarchical clustering.hovertext
(optional): Custom hover text for the data points.
Examples
Basic Dendrogram
The following example demonstrates how to create a dendrogram using Plotly’s figure_factory
module with randomly generated data.
import plotly.figure_factory as ffimport numpy as np# Generate random datanp.random.seed(42)data = np.random.rand(10, 4) # 10 samples, 4 features# Create dendrogramfig = ff.create_dendrogram(data, labels=[f"Sample {i}" for i in range(10)])fig.update_layout(title="Hierarchical Clustering Dendrogram")fig.show()
The output will be:
The x-axis represents the labels of the data points, and the y-axis represents the clustering distance. Branches merge as clusters combine, illustrating hierarchical relationships between data points.
Dendrogram with Custom Colors
This example customizes the colors of the dendrogram branches.
import plotly.figure_factory as ffimport numpy as np# Generate random datanp.random.seed(42)data = np.random.rand(10, 4)# Create dendrogram with a custom colorscalefig = ff.create_dendrogram(data, labels=[f"Sample {i}" for i in range(10)], colorscale=["#636EFA", "#EF553B", "#00CC96"])fig.update_layout(title="Custom Colored Dendrogram")fig.show()
The output will be:
Horizontal Dendrogram
This example demonstrates a dendrogram with a horizontal orientation.
import plotly.figure_factory as ffimport numpy as np# Generate random datanp.random.seed(42)data = np.random.rand(10, 4)# Create horizontal dendrogramfig = ff.create_dendrogram(data, labels=[f"Sample {i}" for i in range(10)], orientation="right")fig.update_layout(title="Horizontal Dendrogram")fig.show()
The output will be:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Plotly on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours