• pandas基础绘图函数


    pandas基础绘图函数

    Series和DataFrame都有一个用于生成各类图表的plot方法。默认情况下,它们所生成的是线形图。

    • Series画图,默认会用index作为x轴的数据,values作为y轴的数据。
    • dataframe也是用index做x轴,每一列的数据做y轴,一列就是一条曲线。
    • pandas通过plot(kind=‘line’)通过kind来修改绘图的类型,默认是线形图

    解决中文和负号下的重要技巧:

    • matplotlib的默认字体不是中文,所以不能显示中文

      plt.rcParams['font.sans-serif']='SimHei'

    • 解决中文字体下,负号无法正常显示的问题

      plt.rcParams['axes.unicode_minus']=False

    线形图(折线图)

    简单的Series图表示例,plot()

    import numpy as np
    import pandas as pd
    from pandas import Series,DataFrame
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    s = Series(data=np.random.randint(0, 10, size=10))
    s.plot()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    简单的DataFrame图表示例,plot()

    • 图例的位置可能会随着数据的不同而不同

      index=['张三','李四','王五','赵六']
      columns=['语文','数学','英语','python']
      data=np.random.randint(80,150,size=(4,4))
      df = DataFrame(data=data,columns=columns,index=index)
      df
      '''
      out:
      	语文	数学	英语	python
      张三	136	124	137	122
      李四	82	87	111	146
      王五	134	92	85	82
      赵六	96	126	101	103
      '''
      df
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

    在这里插入图片描述

    柱状图(条形图)

    Series柱状图示例,kind=‘bar’/‘barh’

    s.plot(kind='barh')
    
    • 1

    在这里插入图片描述

    DataFrame柱状图示例

    df.plot(kind='bar')
    
    • 1

    在这里插入图片描述

    案例:读取文件tips.csv,查看每天的聚会人数情况,每天各种聚会规模的比例

    求和df.sum(),注意灵活使用axis

    tips = pd.read_csv('../data/tips.csv')
    tips
    
    • 1
    • 2

    在这里插入图片描述

    tips.set_index(key='day', inplace=True)
    tips
    
    • 1
    • 2

    在这里插入图片描述

    tips.plot(kind='bar')
    在这里插入图片描述

    tips/tips.sum(axis=1)  # 不能这么除,除完全是NaN
    result = tips.div(tips.sum(axis=1), axis='index')
    result.plot(kind='bar')
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    直方图

    默认的直方图y轴式数据出现的次数

    histogram

    直方图反映数据的分布情况

    n = np.array([1, 1, 2, 3, 3, 4, 5, 5, 6, 6])
    s = Series(data=n)
    s.plot(kind='hist')
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    # 计算直方图数据,返回出现的次数和组距
    np.histogram(n)
    '''
    out: (array([2, 0, 1, 0, 2, 0, 1, 0, 2, 2], dtype=int64),
     array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ]))
    '''
    n  # out: array([1, 1, 2, 3, 3, 4, 5, 5, 6, 6])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    rondom生成随机数百分比直方图,调用hist方法

    • 柱高表示数据的频数,柱宽表示各组数据的组距
    • 参数bins可以设置直方图方柱的个数上限,越大柱宽越小,数据分组越细致
    • 设置density参数为True,可以把频数转换为概率
    # 加上density,变成概率密度,概率/组距
    s.plot(kind='hist', bins=10, density=True)
    
    • 1
    • 2

    在这里插入图片描述

    kde图:核密度估计,用于弥补直方图由于参数bins设置的不合理导致的精度缺失问题

    # 经常把直方图和kde图画在一起。
    # kde:kernel density estimate
    s.plot(kind='hist',bins=10,density=True)![14]
    s.plot(kind='kde')
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    案例:由两个不同的正态分布组成的双峰分布

    n1 = np.random.normal(loc=10, scale=3, size=1000)
    n2 = np.random.normal(loc=30, scale=6, size=1000)
    data = np.hstack(n1, n2)
    
    s = Series(data=data)
    s.plot(kind='hist', density=True, bins=100)
    s.plot(kind='kde')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    散点图

    散点图是观察两个一维数列之间的关系的有效方法,DataFrame对象可用

    使用方法:设置kind=‘scatter’,给明标签columns

    x = np.linspce(2, 8, 10)
    y = np.linspce(0, 10, 10)
    display(x,y)
    '''
    out:
    array([2.        , 2.66666667, 3.33333333, 4.        , 4.66666667,
           5.33333333, 6.        , 6.66666667, 7.33333333, 8.        ])
    array([ 0.        ,  1.11111111,  2.22222222,  3.33333333,  4.44444444,
            5.55555556,  6.66666667,  7.77777778,  8.88888889, 10.        ])
    '''
    plt.scatter(x,y)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    df = DataFrame({'x':x, 'y':y})
    df
    '''
    out:
    		x			y
    0	2.000000	0.000000
    1	2.666667	1.111111
    2	3.333333	2.222222
    3	4.000000	3.333333
    4	4.666667	4.444444
    5	5.333333	5.555556
    6	6.000000	6.666667
    7	6.666667	7.777778
    8	7.333333	8.888889
    9	8.000000	10.000000
    '''
    df.plot(x='x', y='y', kind='scatter')  # 结果图同上
    
    # 正弦图
    x = np.linspace(0, 2*np.pi, 100)
    y = np.sin(x)
    df = DataFrame({'x':x, 'y':y})
    df.plot(x='x', y='y', kind='scatter')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    散点图矩阵,当有多个点时,两两点的关系

    使用函数:pd.plotting.scater_matrix()

    • 参数diagnol:设置对角线的图像类型
    # 观察dataframe列与列两两形成的关系,可以画一个散点图矩阵
    df = DataFrame({'A':np.random.randn(300),'B':np.random.randn(300),'C':np.random.randn(300),'D':np.random.randn(300)})
    df
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    _=pd.plotting.scatter_matrix(df, figsize=(16,16),alpha=1,diagonal='kde')
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    第9期ThreadX视频教程:自制个微秒分辨率任务调度实现方案(2023-10-11)
    CUDA编程- 矩阵乘法
    优维EasyOps®全平台又一波新功能上线,操作体验更带劲
    使用 .NET Core 实现微服务(带例子)
    Centos7 Shell编程之概述、变量(常用系统变量、自定义变量、特殊变量)
    Python中的进程池及进程池锁:multiprocessing.Pool及multiprocessing.Manager().Lock()
    c++视觉--通道分离,合并处理,在分离的通道中的ROI感兴趣区域里添加logo图片
    支付宝常用接口统一封装,可直接支付参数使用(适用于H5、PC、APP)
    gitlab将本地文件项目上传至gitlab服务
    Linux时间同步服务NTP和Chrony
  • 原文地址:https://blog.csdn.net/li_19186535821/article/details/126807854