Python的数据可视化
源博客:
令x作为数据(50个点,每个30维),仅可视化前两维。labels为其类别(假设有三类)。
这里的x就用random来了,具体数据具体分析。
label设定为[1:20]->1, [21:35]->2, [36:50]->3,(python中数组连接方法:先强制转为list,用+,再转回array)。用matplotlib的scatter绘制散点图,代码如下:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.random.rand(50,30)
#
f1 = plt.figure(1)
plt.subplot(211)
plt.scatter(x[:,1],x[:,0])
# with label
plt.subplot(212)
label = list(ones(20))+list(2*ones(15))+list(3*ones(15))
label = array(label)
plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)
# with legend
f2 = plt.figure(2)
idx_1 = np.where(label == 1)
p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30)
idx_2 = np.where(label==2)
p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50)
idx_3 = np.where(label==3)
p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15)
plt.legend(loc = 'upper right')
plt.show()
绘出图形如下所示:
第二个图:
后记:转发的原因是源博客中有一点小问题,无法直接运行,小作修改,可以直接粘贴运行~