给出了以下示例,它来自:https://python-graph-gallery.com/404-dendrogram-with-heat-map/
它生成一个树状图,其中我假设它是基于枕木。
# 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')
del df.index.name
df
# Default plot
sns.clustermap(df)问:如何才能得到非图形形式的树状图?
背景信息:从树状图的根开始,我想以最大的长度切割它。例如,我们有一条从根到左簇(L)的边,一条到右簇(R)的边,...from,这两条边,我想要得到它们的边长,并在这两条边中最长地切出整个树状图。
诚挚的问候
发布于 2018-10-21 18:27:08
clustermap返回ClusterGrid对象的句柄,其中包括每个树状图、h.dendrogram_col和h.dendrogram_row的子对象。其中包括树状图本身,它提供了根据scipy.hierarchical.dendrogram返回数据的树状图几何图形,您可以从中计算特定分支的长度。
h = sns.clustermap(df)
dgram = h.dendrogram_col.dendrogram
D = np.array(dgram['dcoord'])
I = np.array(dgram['icoord'])
# then the root node will be the last entry, and the length of the L/R branches will be
yy = D[-1]
lenL = yy[1]-yy[0]
lenR = yy[2]-yy[3]连接矩阵(用于计算树状图的输入)也可能有助于:
h.dendrogram_col.linkage
h.dendrogram_row.linkagehttps://stackoverflow.com/questions/52915963
复制相似问题