Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีปรับความยาวกิ่งของ Dendrogram ใน Matplotlib


ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สุ่มตัวอย่างข้อมูล (a และ b) จากการกระจายแบบปกติหลายมิติ
  • รวมอาร์เรย์ตามแกนที่มีอยู่ด้วย concatenate()
  • ทำ Hierarchical/Agglomerative Clustering
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่ด้วย figure()
  • เพิ่ม axes เข้าไปใน figure เป็นส่วนหนึ่งของ subplot arrangement
  • พล็อต hierarchical clustering เป็น dendrogram ด้วย dendrogram()
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

ตัวอย่างโค้ด

from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

a = np.random.multivariate_normal([0, 10], [[3, 1], [1, 4]], size=[2, ])
b = np.random.multivariate_normal([0, 10], [[3, 1], [1, 4]], size=[3, ])
X = np.concatenate((a, b), )
Z = linkage(X)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
dendrogram(Z, ax=ax)
plt.show()

ผลลัพธ์

วิธีปรับความยาวกิ่งของ Dendrogram ใน Matplotlib