局部整体(六)利用python绘制树状图
树状图( Dendrogram)简介

由一个根节点组成,根节点产生多个通过分支连接的子节点。常用于表示层次结构或显示聚类算法的结果。树状图既可以看明白数据的层次结构,也能明白指标间的「对比」。
快速绘制
-
基于dendrogram
import pandas as pd from matplotlib import pyplot as plt from scipy.cluster.hierarchy import dendrogram, linkage import numpy as np # 导入数据 url = 'https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/mtcars.csv' df = pd.read_csv(url) df = df.set_index('model') df = df.reset_index(drop=True) # 计算样本距离:对df执行层次聚类,并将结果存储在Z中 # Ward方差最小化算法用来计算样本距离 Z = linkage(df, 'ward') # 树状图 dendrogram(Z, labels=df.index, leaf_rotation=90) # 标题 plt.title('Hierarchical Clustering Dendrogram') # 轴标签 plt.xlabel('sample index') plt.ylabel('distance (Ward)') plt.show()
-
基于pyecharts
# 基本树图 from pyecharts import options as opts from pyecharts.charts import Tree # 自定义数据 data = [ { "children": [ { "name": "B"}, { "children": [{ "children": [{ "name": "I"

利用python绘制树状图&spm=1001.2101.3001.5002&articleId=142392096&d=1&t=3&u=e6943973470c4916a0911d5458fd3a03)
1792

被折叠的 条评论
为什么被折叠?



