zoukankan      html  css  js  c++  java
  • Python数据可视化——散点图

    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中稍有不同。详见代码。


    x = rand(50,30)
    from numpy import *
    import matplotlib
    import matplotlib.pyplot as plt
    
    #basic
    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 = find(label==1)
    p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30)
    idx_2 = find(label==2)
    p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50)
    idx_3 = find(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')
    


    result:

    figure(1):


    figure(2):






  • 相关阅读:
    自定判断代码的执行环境
    将某个Qt4项目升级到Qt5遇到的问题[转]
    QT 智能提示设置
    c++基础 explicit
    Qt 5.2 Creator 和 vs2012 QT 插件的安装
    servlet乱码问题总结
    c++基础 使用智能指针
    Servlet学习之web服务器Tomcat 详解
    Windows数据类型
    友元类
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7045428.html
Copyright © 2011-2022 走看看