python:matplotlib画散点图scatter
库:numpy, matplotlib
代码:
# -*-coding: utf-8 -*-
from numpy import *
import matplotlib.pyplot as plt
# 要用到numpy中的array()
# 20个10维的
x = random.rand(20,10)
# ones(n): n个1 i*ones(m): m个i
label = list(ones(10)) + list(2*ones(5)) + list(3*ones(5))
# change to array
label = array(label)
# 设定图
fig = plt.figure()
# 选x的第一维和第二维,散点大小受标签影响最小120,颜色按标签
plt.scatter(x[:,0], x[:,1], 120*label, label)
plt.show()
# 选x的第三维和第四维,散点大小固定120,颜色按标签
plt.scatter(x[:,2], x[:,3], 120, label)
plt.show()
plt.scatter((datingDataMat[:,1],datingDataMat[:,2],c=datingLabels,s=35,alpha=0.4,marker=’o’)
c:散点的颜色
s:散点的大小
alpha:是透明程度
make:散点样式