python自定义输出颜色_Python中SciPy树形图的自定义集群颜色(link_color_func?)

我想用我用字典形式(即{leaf:color})的颜色映射来对我的集群进行着色.

我试过以下https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/,但颜色被搞砸了一些原因.默认情况下看起来不错,我只想分配那些颜色.我看到有一个link_color_func,但是当我尝试使用我的颜色映射(D_leaf_color字典)时,我有一个错误b / c它不是一个功能.我创建了D_leaf_color以自定义与特定集群相关联的叶子的颜色.在我的实际数据集中,这些颜色意味着什么,所以我要从任意颜色分配转向.

我不想在我的实际数据中使用color_threshold b / c,我有更多的集群和SciPy重复颜色,因此这个问题. . .

如何使用我的叶色字典来自定义我的树形图簇的颜色?

# 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值