如何在 Python 中制作用于聚类的散点图?
matplotlibpythondata visualization
要在 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()
输出
它将产生以下输出