Custom cluster colors of SciPy dendrogram in Python (link_color_func?)
<p>I want to color my clusters with a color map that I made in the form of a dictionary (i.e. <code>{leaf: color}</code>).  </p>

<p>I've tried following <a href="https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/" rel="noreferrer">https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/</a> but the colors get messed up for some reason.  The default plot looks good, I just want to assign those colors differently.  I saw that there was a <code>link_color_func</code> but when I tried using my color map (<code>D_leaf_color</code> dictionary) I got an error b/c it wasn't a function.  I've created <code>D_leaf_color</code> to customize the colors of the leaves associated with particular clusters.  In my actual dataset, the colors mean something so I'm steering away from arbitrary color assignments. </p>

<p>I don't want to use <code>color_threshold</code> b/c in my actual data, I have way more clusters and <code>SciPy</code> repeats the colors, hence this question. . .  </p>

<p><strong>How can I use my leaf-color dictionary to customize the color of my dendrogram clusters?</strong> </p>

<p>I made a GitHub issue <a href="https://github.com/scipy/scipy/issues/6346" rel="noreferrer">https://github.com/scipy/scipy/issues/6346</a> where I further elaborated on the approach to color the leaves in  <a href="https://stackoverflow.com/questions/38166327/interpreting-the-output-of-scipys-hierarchical-clustering-dendrogram-maybe-fo">Interpreting the output of SciPy&#39;s hierarchical clustering dendrogram? (maybe found a bug...)</a> but I still can't figure out how to actually either: (i)  use dendrogram output to reconstruct my dendrogram with my specified color dictionary or (ii) reformat my <code>D_leaf_color</code> dictionary for the <code>link_color_func</code> parameter. </p>

<pre><code># Init
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()

# Load data
from sklearn.datasets import load_diabetes

# Clustering
from scipy.cluster.hierarchy import dendrogram, fcluster, leaves_list
from scipy.spatial import distance
from fastcluster import linkage # You can use SciPy one too

%matplotlib inline

# Dataset
A_data = load_diabetes().data
DF_diabetes = pd.DataFrame(A_data, columns = ["attr_%d" % j for j in range(A_data.shape[1])])

# Absolute value of correlation matrix, then subtract from 1 for disimilarity
DF_dism = 1 - np.abs(DF_diabetes.corr())

# Compute average linkage
A_dist = distance.squareform(DF_dism.as_matrix())
Z = linkage(A_dist,method="average")

# Color mapping
D_leaf_colors = {"attr_1": "#808080", # Unclustered gray

                 "attr_4": "#B061FF", # Cluster 1 indigo
                 "attr_5": "#B061FF",
                 "attr_2": "#B061FF",
                 "attr_8": "#B061FF",
                 "attr_6": "#B061FF",
                 "attr_7": "#B061FF",

                 "attr_0": "#61ffff", # Cluster 2 cyan
                 "attr_3": "#61ffff",
                 "attr_9": "#61ffff",
                 }

# Dendrogram
# To get this dendrogram coloring below  `color_threshold=0.7`
D = dendrogram(Z=Z, labels=DF_dism.index, color_threshold=None, leaf_font_size=12, leaf_rotation=45, link_color_func=D_leaf_colors)
# TypeError: 'dict' object is not callable
</code></pre>

<p><a href="https://i.stack.imgur.com/5jEie.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5jEie.png" alt="enter image description here"></a></p>

<p>I also tried <a href="https://stackoverflow.com/questions/16883412/how-do-i-get-the-subtrees-of-dendrogram-made-by-scipy-cluster-hierarchy">how do I get the subtrees of dendrogram made by scipy.cluster.hierarchy</a></p>