如何用Python製作用於聚類的散點圖?


用Python製作用於聚類的散點圖,我們可以採取以下步驟:

  • 設定圖形大小並調整子圖之間和周圍的填充。
  • 使用numpy建立x和y資料點,聚類和中心。
  • 建立一個新圖形或啟用一個現有圖形。
  • 向當前圖形新增一個子圖排列。
  • 使用scatter()方法繪製散點資料點。
  • 迭代中心資料並使用scatter()方法放置標記。
  • 要顯示圖形,請使用show()方法。

示例

import numpy as np
import matplotlib.pyplot as plt

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

x = np.random.randn(10)
y = np.random.randn(10)
Cluster = np.array([0, 1, 1, 1, 3, 2, 2, 3, 0, 2])
centers = np.random.randn(4, 2)

fig = plt.figure()
ax = fig.add_subplot(111)

scatter = ax.scatter(x, y, c=Cluster, s=50)
for i, j in centers:
   ax.scatter(i, j, s=50, c='red', marker='+')

plt.show()

輸出

將產生以下輸出

更新日期:2021年9月19日

7000多次瀏覽

開啟你的職業生涯

透過完成課程獲取認證

開始
廣告
© . All rights reserved.