Custom cluster colors of SciPy dendrogram in Python (link_color_func?)

后端 未结 3 1297
感情败类
感情败类 2020-12-28 08:42

I want to color my clusters with a color map that I made in the form of a dictionary (i.e. {leaf: color}).

I\'ve tried following https://joernhees.de/

3条回答
  •  星月不相逢
    2020-12-28 09:08

    Two-liner for applying custom colormap to cluster branches:

    import matplotlib as mpl
    from matplotlib.pyplot import cm
    from scipy.cluster import hierarchy
    
    cmap = cm.rainbow(np.linspace(0, 1, 10))
    hierarchy.set_link_color_palette([mpl.colors.rgb2hex(rgb[:3]) for rgb in cmap])
    

    You can then replace rainbow by any cmap and change 10 for the number of cluster you want.

提交回复
热议问题