擅长:python、mysql、java
<p><a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.dendrogram.html" rel="nofollow noreferrer">^{<cd1>}</a>使用matplotlib创建绘图,因此在调用了<code>dendrogram</code>之后,可以随意操作绘图。特别是,可以修改x轴标签的属性,包括颜色。下面是一个例子:</p>
<pre><code>import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt
mat = np.array([[1.0, 0.5, 0.0],
[0.5, 1.0, -0.5],
[1.0, -0.5, 0.5],
[0.0, 0.5, -0.5]])
dist_mat = mat
linkage_matrix = linkage(dist_mat, "single")
plt.clf()
ddata = dendrogram(linkage_matrix,
color_threshold=1,
labels=["a", "b", "c", "d"])
# Assignment of colors to labels: 'a' is red, 'b' is green, etc.
label_colors = {'a': 'r', 'b': 'g', 'c': 'b', 'd': 'm'}
ax = plt.gca()
xlbls = ax.get_xmajorticklabels()
for lbl in xlbls:
lbl.set_color(label_colors[lbl.get_text()])
plt.show()
</code></pre>
<p>下面是这个例子产生的情节:</p>
<p><img src="https://i.stack.imgur.com/LDJ9k.png" alt="example plot"/></p>