Advertisement

Python数据可视化入门

阅读量:

python中数据可视化基础

    • 1、numpy 模块
      • 2、 pandas 模块
      • 3、数据读取与导入
      • 4、matolotlib 模块

本篇文章重点介绍Python语言在数据可视化方面的基础知识,所涵盖的内容涉及numpy、pandas以及matolotlib等模块。
上一篇: python数据分析之pandas

1、numpy模块功能与应用

复制代码
    # -*- coding: utf-8 -*-
    # --------------------------------------
    # @Time    : 2019/10/14 14:32
    # @Author  : hxf
    # @Email   : 1870212598@qq.com
    # @File    : shuju1_numpy.py
    # Description :numpy模块
    # ----------------------------------
    '''
    1、创建数组格式
    	一维数组:numpy.array([元素1,元素2,元素3,...,元素n])
    	二维数据:numpy.array([[元素1,元素2,...,元素n],[元素1,元素2,...,元素n],[元素1,元素2,...,元素n]])
    2、数组排序
    	sort()
    3、数组取最大值和最小值
    	max()、min()
    4、数组的切片操作
    	数组[起始下标:最终下标+1]
    
    '''
    import numpy as np
    
    #创建数组
    print('---------------------一维数组---------------------')
    x=np.array(["a","8","9","12"])
    print(x)
    print(type(x))
    
    print('---------------------二维数组----------------------')
    y=np.array([[1,7,3],[5,8,2],[9,4,7]])
    print(y)
    print(type(y))
    print('---------------------取特定元素---------------------')
    print(x[2])
    print(y[1][2])
    
    #数组排序
    print('---------------------排序后的数组---------------------')
    x.sort()
    print(x)
    y.sort()
    print(y)
    
    #数组最大值和最小值
    print('---------------------数组最大值和最小值---------------------')
    y1=y.max() #list中也有求max和min,但数组的效率高
    y2=y.min()
    print('y的最大值:',y1)
    print('y的最小值:',y2)
    
    #数组的切片操作
    print(x[1:3]) #1-2号元素
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

2、pandas模块功能与应用

复制代码
    '''
    panda模块:
    	Series #index 索引
    	DataFrame # columns列名
    '''
    import pandas as pda
    
    print('--------------------------Series创建一串数据-----------------------------')
    a=pda.Series([8,9,2,1])
    print(a)
    '''
    0    8
    1    9
    2    2
    3    1
    dtype: int64
    '''
    #指定索引
    b=pda.Series([8,9,2,1],index=['One','Two','Three','Four'])
    print(b)
    '''
    One      8
    Two      9
    Three    2
    Four     1
    dtype: int64
    '''
    print('--------------------------DataFrame 创建数据框-----------------------------')
    c=pda.DataFrame([[5,6,2,3],[8,4,6,3],[6,4,31,2]])
    print(c)
    '''
       0  1   2  3
    0  5  6   2  3
    1  8  4   6  3
    2  6  4  31  2
    '''
    print('-------------------------------------------------------')
    d=pda.DataFrame([[5,6,2,3],[8,4,6,3],[6,4,31,2]],columns=['One','Two','Three','Four'])#指定列名
    print(d)
    '''
       One  Two  Three  Four
    0    5    6      2     3
    1    8    4      6     3
    2    6    4     31     2
    '''
    print('-------------------------------------------------------')
    e=pda.DataFrame({                   # 通过字典创建数据
    	"one":4,
    	"two":[6,2,3],
    	"three":list(str(982))
    })
    print(e)
    '''
    one  two three
    0    4    6     9
    1    4    2     8
    2    4    3     2
    '''
    print('-------------------------------------------------------')
    print(d.head())      # 头部数据,默认前5行
    '''
       One  Two  Three  Four
    0    5    6      2     3
    1    8    4      6     3
    2    6    4     31     2
    '''
    print('-------------------------------------------------------')
    print(d.tail(2))     # 取后两行
    '''
       One  Two  Three  Four
    1    8    4      6     3
    2    6    4     31     2
    '''
    print('-------------------------------------------------------')
    print(d.describe())  # 按列统计描述
    '''
            One       Two      Three      Four
    count  3.000000  3.000000   3.000000  3.000000
    mean   6.333333  4.666667  13.000000  2.666667
    std    1.527525  1.154701  15.716234  0.577350
    min    5.000000  4.000000   2.000000  2.000000
    25%    5.500000  4.000000   4.000000  2.500000
    50%    6.000000  4.000000   6.000000  3.000000
    75%    7.000000  5.000000  18.500000  3.000000
    max    8.000000  6.000000  31.000000  3.000000
    '''
    #转置
    print('-------------------------------------------------------')
    print(d.T) # 转置
    '''
       0  1   2
    One    5  8   6
    Two    6  4   4
    Three  2  6  31
    Four   3  3   2
    '''
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

3、数据读取与导入

复制代码
    # ----------------------------------
    '''
    数据导入
    	导入csv文件
    	导入excel文件
    	导入MySQLS数据库中的数据
    	导入html数据
    	导入文本数据 read_table
    '''
    import pandas as pda
    print('----------------------------------读取csv文件------------------------------------------------')
    i=pda.read_csv('data/hexun.csv')
    print('----------------------------------------------------------------------------------')
    print(i)
    print(i.describe())
    print('----------------------------------------------------------------------------------')
    print(i.sort_values(by="hits"))
    
    print('----------------------------------读取excel文件------------------------------------------------')
    j=pda.read_excel('data/hexun.xls')
    print('----------------------------------------------------------------------------------')
    print(j)
    print(j.describe())
    print('----------------------------------------------------------------------------------')
    print(j.sort_values(by="hits"))
    
    print('----------------------------------读取html数据------------------------------------------------')
    k=pda.read_html('data/abc.html')
    print(k)
    l=pda.read_html('https://book.douban.com/')
    print(l)
    
    print('----------------------------------读取txt文件------------------------------------------------')
    m=pda.read_table('data/abc.txt')
    print(m)
    
    '''
    print('----------------------------------读取sql文件------------------------------------------------')
    import pymysql
    conn=pymysql.connect(host="127.0.0.1",user="root",passwd="123456",db="hexun")
    sql="select * from myhexun"
    n=pda.read_sql(sql,conn)
    print(n)
    '''
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

4、matolotlib 模块

复制代码
    '''
    matplotlib.pyplot模块
    	1、折线图和散点图plot  plot(x轴数据,y轴数据,展现形式),参数是可以叠加的
    		颜色参数:
    			c-cyan--青色
    			r-red--红色
    			m-magente--品红
    			g-green--绿色
    			b-blue--蓝色
    			y-yellow--黄色
    			k-black--黑色
    			w-white--白色
    		线条样式:
    			-  直线
    			-- 虚线
    			-. -.式
    			:  细小虚线
    		点的样式:
    			s--方形
    			h--六角形
    			H--六角形
    			*--星形
    			+--加号
    			x--x形
    			d--菱形
    			D--菱形
    			p--五角星
    	2、直方图hist
    		随机数的生成
    		直方图绘制 hist(data,sty,histtype)  # 数据 形式 轮廓参数
    	3、子图绘图
    		subplot(2,2,1)
    	4、可视化分析案例
    		读取和讯博客的数据并可视化分析
    
    
    '''
    '''
    import matplotlib.pyplot as pyl
    import numpy as np
    
    # 1、折线图和散点图plot
    x=[1,2,3,4,8]
    y=[5,7,2,1,5]
    '''
    
    '''
    pyl.plot(x,y)          # 折线图
    pyl.show()
    pyl.plot(x,y,'o')      # 散点图,会叠加
    pyl.show()
    
    pyl.plot(x,y,'oc')     # 青色的散点图
    pyl.show()
    
    pyl.plot(x,y,'-')      # 直线
    pyl.show()
    
    pyl.plot(x,y,'--')       # 虚线
    pyl.show()
    
    pyl.plot(x,y,'-.')       # 虚线
    pyl.show()
    
    pyl.plot(x,y,':')       # 细小虚线
    pyl.show()
    
    pyl.plot(x,y,'*')       # 星形
    pyl.show()
    
    pyl.plot(x,y,'d')       # 菱形
    pyl.show()
    
    pyl.plot(x,y,'h')       # 六角形
    pyl.show()
    '''
    
    '''
    pyl.plot(x,y)
    x2=[1,3,6,8,10,12,19]
    y2=[1,6,9,10,19,23,35]
    pyl.plot(x2,y2)
    
    pyl.title("show")         # 添加标题
    pyl.xlabel("ages")        # 添加x轴标题
    pyl.ylabel("temp")        # 添加y轴标题
    pyl.xlim(0,20)            # 设置x轴范围
    pyl.ylim(0,35)            # 设置y轴范围
    pyl.show()
    '''
    
    '''
    # 2、直方图hist
    # 随机数的生成
    import numpy as np
    data=np.random.random_integers(1,20,10) #(最小值,最大值,个数) 生成随机整数
    # print(data)
    data2=np.random.normal(5.0,2.0,10) # (均值,西格玛,个数) 生成正态分布的随机数
    # print(data2)
    
    data3=np.random.normal(10.0,1.0,10000)
    # pyl.hist(data3)
    # pyl.show()
    
    data4=np.random.random_integers(1,25,1000)
    # print(data4)
    # pyl.hist(data4)
    sty=np.arange(1,30,2)
    # pyl.hist(data4,sty)
    # pyl.hist(data4,sty,histtype='stepfilled')
    # pyl.hist(data4,histtype='stepfilled')
    # pyl.show()
    # 更多随机数生成更多知识参考http://www.mamicode.com/info-detail-507676.html
    
    #3、柱状图
    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    
    num_list = [1.5, 0.6, 7.8, 6]
    plt.bar(range(len(num_list)), num_list)
    plt.show()
    
    # 4、子图绘图
    pyl.subplot(2,2,1)
    x1=[1,2,3,4,5]
    y1=[5,3,5,23,5]
    pyl.plot(x1,y1)
    
    pyl.subplot(2,2,2)
    x2=[5,2,3,8,6]
    y2=[7,9,12,12,3]
    pyl.plot(x2,y2)
    
    pyl.subplot(2,1,2)
    x3=[5,6,7,10,19,20,29]
    y3=[6,2,4,21,5,1,5]
    pyl.plot(x3,y3)
    pyl.show()
    
    '''
    '''
    5.可视化分析案例
    	读取和讯博客的数据并可视化分析
    '''
    # 读取和讯博客的数据并可视化分析
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    data=pd.read_csv('data/myhexun.csv')
    # print(data.shape) # 查看数据结构
    # print(data.values)  # 查看 values[第几行][第几列]
    # print(data.values[0])
    # print(data.values[1][1])
    
    # 转置
    data2=data.T
    y1=data2.values[3] # 获取阅读数
    x1=data2.values[4] # 获取评论数
    # plt.plot(x1,y1)
    # plt.show()
    x2=data2.values[0] # 取id
    # plt.plot(x2,y1)
    # plt.show()
    plt.plot(x2,x1)
    plt.show()
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

下一篇文章:数据探索与数据清洗

全部评论 (0)

还没有任何评论哟~