#405 Dendrogram with heatmap and coloured leaves

 

 

 

 

The chart #404 describes in detail how to do a dendrogram with heatmap using seaborn. I strongly advise to read it before doing this chart. Once you understood how to study the structure of your population, you probably want to compare it with your expectation.

Here I use the mtcars dataset that gives the features of several cars through a few numerical variables. I represent how these cars are clustered. Then, I add a color sheme on the left part of the plot. The 3 colours represent the 3 possible values of the ‘cyl’ column. Now, you know if this column explain the structure of our car population!

 

 

 

 

 


# Libraries
import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt

# Data set
url = 'https://python-graph-gallery.com/wp-content/uploads/mtcars.csv'
df = pd.read_csv(url)
df = df.set_index('model')
df

# Prepare a vector of color mapped to the 'cyl' column
my_palette = dict(zip(df.cyl.unique(), ["orange","yellow","brown"]))
row_colors = df.cyl.map(my_palette)

# plot
sns.clustermap(df, metric="correlation", method="single", cmap="Blues", standard_scale=1, row_colors=row_colors)

Leave a Reply

Your email address will not be published.