GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All
  • Best
  • Libs
  • Learn

This view

  • Original page
  • New address

GeezerVison is showing python-graph-gallery.com — Basic Dendrogram

Basic Dendrogram

logo of a chart:Dendrogram

This post aims to describe how to draw a basic dendrogram with scipy library of python.

Dendrogram sectionAbout this chart

To draw a dendrogram, you first need to have a numeric matrix. Each line represents an entity (here a car). Each column is a variable that describes the cars. The objective is to cluster the entities to show who shares similarities with whom. The dendrogram will draw the similar entities closer to each other in the tree.

Let’s start by loading a dataset and the requested libraries:

# Libraries
import pandas as pd
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
import numpy as np
 
# Import the mtcars dataset from the web + keep only numeric variables
url = 'https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/mtcars.csv'
df = pd.read_csv(url)
df = df.set_index('model')
df = df.reset_index(drop=True)
df.head()
mpgcyldisphpdratwtqsecvsamgearcarb
021.06160.01103.902.62016.460144
121.06160.01103.902.87517.020144
222.84108.0933.852.32018.611141
321.46258.01103.083.21519.441031
418.78360.01753.153.44017.020032

All right, now that we have our numeric matrix, we can calculate the distance between each car, and draw the hierarchical clustering. Distance calculation can be done by the linkage() function. I strongly advise you to visit the next page for more details concerning this crucial step.

# Calculate the distance between each sample
# You have to think about the metric you use (how to measure similarity) + about the method of clusterization you use (How to group cars)
Z = linkage(df, 'ward')

Last but not least, you can easily plot this object as a dendrogram using the dendrogram() function of scipy library. These parameters are passed to the function:

  • Z : The linkage matrix
  • labels : Labels to put under the leaf node
  • leaf_rotation : Specifies the angle (in degrees) to rotate the leaf labels

See post #401 for possible customisations to a dendrogram.

Basic Dendrogram

# Plot title
plt.title('Hierarchical Clustering Dendrogram')

# Plot axis labels
plt.xlabel('sample index')
plt.ylabel('distance (Ward)')

# Make the dendrogram
dendrogram(Z, labels=df.index, leaf_rotation=90)

# Show the graph
plt.show()
Dataviz caveat card: Boxplots hide information

⚠️ Avoid the 61 most common dataviz mistakes

The dataviz caveats I see the most, as a deck of 61 cards: a chart that is not ideal, and a better alternative. Free.

I respect your privacy. Unsubscribe anytime.

Related posts

Customised dendrogram

Customised dendrogram

Color dendrogram labels

Color dendrogram labels

Dendrogram with heat map

Dendrogram with heat map

Dendrogram with heatmap and coloured leaves

Dendrogram with heatmap and coloured leaves

Related chart types

logo of a chart:Tree

Treemap

logo of a chart:Venn

Venn Diagram

logo of a chart:Doughnut

Donut

logo of a chart:Pie

Pie Chart

logo of a chart:Dendrogram

Dendrogram

logo of a chart:CircularPacking

Circular Packing

logo of a chart:Waffle2

Waffle

300 projects

💡 Out of dataviz inspiration?

I have spent years collecting the most beautiful data visualizations out there. Get the list of my top 300 dataviz projects, hand-picked, for free.

I respect your privacy. Unsubscribe anytime.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.