【发布时间】:2018-03-15 22:40:55
【问题描述】:
我很难创建一个散点图,以不同颜色显示不同类的点以及图例中类的相应标签。
我有 52 个样本、2 个特征和 3 个类(1,2 和 3 类)。数据在X数组中,标签在labels数组中。
我想绘制将包含相应颜色的类名(1,2 和 3)的图例。
我的代码:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(52,2)
labels = np.ones(x.shape[0])
labels[0:8] = 1
labels[8:31] = 2
labels[31:] = 3
plt.scatter(x[:,0], x[:,1], c = labels, label = labels)
plt.legend()
plt.show()
结果:
【问题讨论】:
标签: python matplotlib scatter-plot