树状图标签重叠

时间:2019-02-19 05:18:56

标签: python plot label dendrogram

我有一个带有标签(第一行和第一列)的二维关系数据数组。

当我创建树状图时,我的标签重叠了。 如何使标签均匀分开?

enter image description here

file= open(fileName)
line = file.readline()
file.close()
populations=line.split('\t')
del populations[0]


data = np.loadtxt(fileName, delimiter="\t",skiprows=1,usecols=range(1,len(populations)+1 ))

fig, ax = plt.subplots()

Y1 = sch.linkage(data, method='ward',optimal_ordering=True)

Z1 = sch.dendrogram(Y1, orientation='top')

ind1= Z1['leaves']
arr = np.array(populations)
populations = arr[ind1]
ax.set_xticks([])
ax.set_xticks(np.arange(len(populations)))
ax.set_xticklabels(populations )
plt.xticks(rotation=90)

plt.show()

1 个答案:

答案 0 :(得分:0)

我认为在树状图的构造中简单地指定标签可能会更容易,因为它们在构造时就已为人所知,例如以下

import scipy.cluster.hierarchy as sch
import numpy as np           # Only needed for random sample data

np.random.seed(1)            # Seeded for reproducing

populations = np.arange(10)  # Create some random sample data
data = abs(np.random.randn(10))

fig, ax = plt.subplots()

Y1 = sch.linkage(data, method='ward',optimal_ordering=True)
Z1 = sch.dendrogram(Y1, orientation='top', labels=populations)

plt.show()

会给你