Python数据可视化——散点图

转载出处:http://blog.youkuaiyun.com/abcjennifer/article/details/19848269


PS: 翻了翻草稿箱, 发现居然存了一篇去年2月的文章。。。虽然naive,还是发出来吧。。。


本文记录了Python中的数据可视化——散点图scatter,

令x作为数据(50个点,每个30维),我们仅可视化前两维。labels为其类别(假设有三类)。

这里的x就用random来了,具体数据具体分析。

label设定为[1:20]->1, [21:35]->2, [36:50]->3,(python中数组连接方法:先强制转为list,用+,再转回array)

用matplotlib的scatter绘制散点图,legend和matlab中稍有不同,详见代码。


[python]  view plain  copy
  1. x = rand(50,30)  
  2. from numpy import *  
  3. import matplotlib  
  4. import matplotlib.pyplot as plt  
  5.   
  6. #basic  
  7. f1 = plt.figure(1)  
  8. plt.subplot(211)  
  9. plt.scatter(x[:,1],x[:,0])  
  10.   
  11. # with label  
  12. plt.subplot(212)  
  13. label = list(ones(20))+list(2*ones(15))+list(3*ones(15))  
  14. label = array(label)  
  15. plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)  
  16.   
  17. # with legend  
  18. f2 = plt.figure(2)  
  19. idx_1 = find(label==1)  
  20. p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30)  
  21. idx_2 = find(label==2)  
  22. p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50)  
  23. idx_3 = find(label==3)  
  24. p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15)  
  25. plt.legend(loc = 'upper right')  


result:

figure(1):


figure(2):


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值