zoukankan      html  css  js  c++  java
  • python--数据可视化

    数据如何清晰、准确、交互的展现,通过数据可视化,将实现这些效果。

    python可视化需要用到的库:pandas,matplotlib

    参考官方教程:http://matplotlib.org/index.html

    散点图:

    绘图函数:plot(x,y,'.',color(r,g,b))

    x、y,x轴和y轴的序列;‘.’,散点图中点的大小;color:rgb定义

    # -*- coding: utf-8 -*-
    
    import pandas
    import matplotlib
    import matplotlib.pyplot as plt
    
    data = pandas.read_csv(
        'C://Users//leon//Desktop//data.csv'
    )
    
    mainColor = (52/256, 88/256, 151/256, 1) #1是透明度
    
    font = {
        'size': 20,
        'family': 'SimHei'
    }   #设置字体,若不设置则不会显示中文字体
    matplotlib.rc('font', **font)
    
    #%matplotlib qt
    
    #plt.grid(True)
    
    #小点
    plt.xlabel('广告费用', color=mainColor)
    plt.ylabel('购买用户数', color=mainColor)
    plt.tick_params(axis='x', colors=mainColor)  #刻度线设置
    plt.tick_params(axis='y', colors=mainColor)  
    plt.plot(
        data['广告费用'], 
        data['购买用户数'], 
        '.', color=mainColor
    )

    如若运行环境是pycharm,则在最后添加:

    plt.show()
    才可显示图形。

     折线图:

    plot(x,y,style,color,linewidth)

    title('图的标图')

    style,线的样式;linewidth线的宽度。

    import pandas
    import matplotlib
    from matplotlib import pyplot as plt
    
    data = pandas.read_csv(
        'C://Users//leon//Desktop//data.csv'
    )
    
    #对日期格式进行转换
    data['购买日期'] = pandas.to_datetime(
        data['日期']
    )
    
    
    mainColor = (42/256, 87/256, 141/256, 1);
    
    font = {
        'size': 20,
        'family': 'SimHei'
    }
    matplotlib.rc('font', **font)
    
    #%matplotlib qt
    
    plt.xlabel(
        '购买日期', 
        color=mainColor
    )
    plt.ylabel(
        '购买用户数', 
        color=mainColor
    )
    plt.tick_params(
        axis='x', 
        colors=mainColor
    )
    plt.tick_params(
        axis='y', 
        colors=mainColor
    )
    
    #'-'    顺滑的曲线
    plt.plot(
        data['购买日期'], 
        data['购买用户数'], 
        '-', color=mainColor
    )   
    
    plt.title('购买用户数')
    plt.show()

    与散点图相比,plt.plot(第三个参数设置为‘-’,即可转化为则线图。

     饼图:

    pie(x,labels,colors,explode,autopct)

    x 绘图的序列;labels标签序列;colors颜色;explode需要突出的序列;autopct饼图占比的下手格式;%.2f保留2位小数

    labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
    sizes = [15, 30, 45, 10]
    explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')
    
    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
            shadow=True, startangle=90)
    ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

    #设置为横轴和纵轴等长的饼图
    #也就是圆形的饼图,而非椭圆形的饼图

    plt.show()

     柱形图:

    bar(left,height,width,color)

    left x轴序列;height y轴数值 ;width 柱形图的宽度;color 填充颜色

    import numpy
    import pandas
    import matplotlib
    from matplotlib import pyplot as plt
    
    font = {
        'family' : 'SimHei'
    }
    matplotlib.rc('font', **font)    #创建中文环境
    
    data = pandas.read_csv(
        'C://Users//leon//Desktop//data.csv'
    )
    
    result = data.groupby(
        by=['手机品牌'], 
        as_index=False
    )['月消费(元)'].agg({
        '月消费': numpy.sum
    })   #透视
    
    #竖向柱形图
    mainColor = (42/256, 87/256, 141/256, 1)
    index = numpy.arange(
        result.月消费.size
    )
    sgb = result.sort_values(
        by="月消费", 
        ascending=False
    )  #降序,false改为true则为升序
    plt.bar(
        index, sgb.月消费, 
        color=mainColor
    )
    plt.xticks(index, sgb.手机品牌) #xticks函数为加上中文标签
    plt.show()
  • 相关阅读:
    Codeforces 868A Bark to Unlock
    poj 3744 Scout (Another) YYF I
    Codeforces 526F Pudding Monsters
    Codeforces 789D Weird journey
    Codeforces 827E Rusty String
    Codeforces 841D Leha and another game about graph
    bzoj 3560 DZY Loves Math V
    Codeforces 833A The Meaningless Game
    Codeforces 839D Winter is here
    Codeforces 839C Journey
  • 原文地址:https://www.cnblogs.com/leon507/p/7685238.html
Copyright © 2011-2022 走看看