• 绘图和可视化学习笔记2


    2 pandas中的绘图函数

    matplotlib作图比较麻烦,在pandas中就比较容易了。

    2.1 线性图

    s = Series(np.random.randn(10).cumsum(), index=np.arange(0,100,10))
    s.plot()
    

    结果:

    df = DataFrame(np.random.randn(10,4).cumsum(0),columns=list('ABCD'), index=np.arange(0,100,10))
    df.plot()
    

    结果:

    图表示例:

    2.2 柱状图

    指定kind为bar和barh。

    import numpy as np
    import pandas as pd
    from pandas import Series,DataFrame
    import matplotlib.pyplot as plt
    # 两个子图
    fig,axes = plt.subplots(2,1)
    
    data = Series(np.random.rand(16), index=list('abcdefghiljkmnop'))
    # kind:表示类型,bar和barh
    data.plot(kind='bar', ax=axes[0], color='k', alpha=0.7)  # 纵向
    data.plot(kind='barh', ax=axes[1], color='k', alpha=0.7)  # 横向
    

    结果:

    df = DataFrame(np.random.rand(6,4),
                   index=['one','two','three','four','five','six'],
                    columns = pd.Index(['A','B','C','D'], name='Genus')
                   )
    
    df.plot(kind='bar',stacked=True,alpha=0.75)
    # stacked True 表示堆积,False表示不堆积
    # alpha 表示透明度
    

    结果:

    2.3 直方图和密度图

    # 直方图:hist
    df.sum(axis=1).hist(bins=15)
    # 等价于
    df.sum(axis=1).plot(kind='hist')
    
    ## 密度图:kde--标准混合正态分布
    df.sum(axis=1).plot(kind='kde')
    
    df = DataFrame(np.random.rand(26,4),
                   index=list('abcdefghijklmnopqrstuvwxyz'),
                    columns = pd.Index(['A','B','C','D'], name='Genus')
                   )
    df.sum(axis=0).plot(kind='hist')  # 直方图
    df.sum(axis=0).plot(kind='kde')   # 密度
    

    结果:

    2.4 散布图

    scatter方法用于绘制散布图

    # 100个0-1的随机点,传入x,y两个参数
    plt.scatter(np.random.rand(100),np.random.rand(100))
    plt.title('100 Random number')
    

    3 python图形化工具生态系统

    略。

    基于web的图形化工具才是图形化工具的未来。

  • 相关阅读:
    乐观锁悲观锁及其使用场景
    inner join, left join, right join的作用是什么
    主键和唯一索引的区别
    在排序数组中查找元素的第一个和最后一个位置
    寻找旋转排序数组中的最小值
    [模板] 最小费用最大流
    CF878E Numbers on the blackboard
    CF1286F Harry The Potter
    CF1368H1 Breadboard Capacity
    CF1442E Black, White and Grey Tree
  • 原文地址:https://www.cnblogs.com/felo/p/6386402.html
Copyright © 2020-2023  润新知