如何在Python中绘制聚类散点图?
要在Python中绘制聚类散点图,我们可以按以下步骤进行 –
- 设置图形大小并调整子图之间和周围的填充。
- 使用numpy创建x和y数据点,集群和中心。
- 创建一个新的图或激活现有图。
- 将一个子图安排加入到当前图中。
- 使用 scatter() 方法绘制散点数据点。
- 迭代中心数据并使用 scatter() 方法放置标记。
- 使用 show() 方法显示图形。
阅读更多:Python 教程
示例
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()
输出
它将生成以下输出

极客教程