Dendrograms in MATLAB®

How to make Dendrograms in MATLAB® with Plotly.


Plotly is known for its graphing libraries, but it's also a broader ecosystem for building interactive data apps, AI data analysis tools, and deploying and hosting Dash apps. Explore the full set of tools and platforms designed to help turn data into action. Discover Plotly.

Note: We are retiring documentation for R, MATLAB, Julia, and F#. Learn more about this change here.

Plot Dendrogram

Generate sample data.

rng('default')
X = rand(10,3);

Create a hierarchical binary cluster tree using linkage. Then, plot the dendrogram using the default options.

rng('default')
X = rand(10,3);

tree = linkage(X,'average');

dendrogram(tree)

fig2plotly(gcf);

Specify Dendrogram Leaf Node Order

Generate sample data.

rng('default')
X = rand(10,3);

Create a hierarchical binary cluster tree using linkage.

rng('default')
X = rand(10,3);

tree = linkage(X,'average');

D = pdist(X);
leafOrder = optimalleaforder(tree,D);

Plot the dendrogram using an optimal leaf order.

rng('default')
X = rand(10,3);

tree = linkage(X,'average');

D = pdist(X);
leafOrder = optimalleaforder(tree,D);

dendrogram(tree,'Reorder',leafOrder)

fig2plotly(gcf);

The order of the leaf nodes in the dendrogram plot corresponds - from left to right - to the permutation in leafOrder.

Specify Number of Nodes in Dendrogram Plot

Generate sample data.

rng('default')
X = rand(100,2);

There are 100 data points in the original data set, X.

Create a hierarchical binary cluster tree using linkage. Then, plot the dendrogram for the complete tree (100 leaf nodes) by setting the input argument P equal to 0.

rng('default')
X = rand(100,2);

tree = linkage(X,'average');
dendrogram(tree,0)

Now, plot the dendrogram with only 25 leaf nodes. Return the mapping of the original data points to the leaf nodes shown in the plot.

rng('default')
X = rand(100,2);

tree = linkage(X,'average');
dendrogram(tree,0)

[~,T] = dendrogram(tree,25);

fig2plotly(gcf);

List the original data points that are in leaf node 7 of the dendrogram plot.

rng('default')
X = rand(100,2);

tree = linkage(X,'average');
dendrogram(tree,0)

[~,T] = dendrogram(tree,25);

find(T==7)
ans =

     7
    33
    60
    70
    74
    76
    86

Change Dendrogram Orientation and Line Width

Generate sample data.

rng('default')
X = rand(10,3);

Create a hierarchical binary cluster tree using linkage. Then, plot the dendrogram with a vertical orientation, using the default color threshold. Return handles to the lines so you can change the dendrogram line widths.

rng('default')
X = rand(10,3);

tree = linkage(X,'average');
H = dendrogram(tree,'Orientation','left','ColorThreshold','default');
set(H,'LineWidth',2)

fig2plotly(gcf);