zoukankan      html  css  js  c++  java
  • matplotlib可视化直方图

    plt.hist()

    用于画直方图。

    参数列表:

    plt.hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None)

    • x:指定要绘制直方图的数据;输入值,这需要一个数组或者一个序列,不需要长度相同的数组。
    • bins:指定直方图条形的个数;
    • range:指定直方图数据的上下界,默认包含绘图数据的最大值和最小值;
    • density:布尔,可选。如果"True",返回元组的第一个元素将会将计数标准化以形成一个概率密度,也就是说,直方图下的面积(或积分)总和为1。这是通过将计数除以数字的数量来实现的观察乘以箱子的宽度而不是除以总数数量的观察。如果叠加也是“真实”的,那么柱状图被规范化为1。(替代normed)
    • weights:该参数可为每一个数据点设置权重;
    • cumulative:是否需要计算累计频数或频率;
    • bottom:可以为直方图的每个条形添加基准线,默认为0;
    • histtype:指定直方图的类型,默认为bar,除此还有’barstacked’, ‘step’, ‘stepfilled’;
    • align:设置条形边界值的对其方式,默认为mid,除此还有’left’和’right’;
    • orientation:设置直方图的摆放方向,默认为垂直方向;
    • rwidth:设置直方图条形宽度的百分比;
    • log:是否需要对绘图数据进行log变换;
    • color:设置直方图的填充色;
    • label:设置直方图的标签,可通过legend展示其图例;
    • stacked:当有多个数据时,是否需要将直方图呈堆叠摆放,默认水平摆放;
    • normed:是否将直方图的频数转换成频率;(弃用,被density替代)
    • alpha:透明度,浮点数。

    返回值:

    • n:直方图的值
    • bins:返回各个bin的区间范围
    • patches:返回每个bin的信息
    import numpy as np
    import matplotlib.pyplot as plt
    
    np.random.seed(19260801)
    
    mu1, sigma1 = 100, 15
    mu2, sigma2 = 80, 15
    x1 =  np.random.normal(mu1,sigma1,10000) # (均值,标准差,个数)
    x2 =  np.random.normal(mu2,sigma2,10000) 
    
    # the histogram of the data
    # 50:将数据分成50组
    # color:颜色;alpha:透明度
    # density:是密度而不是具体数值
    n1, bins1, patches1 = plt.hist(x1, bins=50,  density=True, color='g', alpha=1)
    n2, bins2, patches2 = plt.hist(x2, bins=50,  density=True, color='r', alpha=0.2)
    
    plt.show()

    添加分布曲线

    根据plt.hist()返回值,很容易画出分布曲线

    #print(len(bins1)) #51
    #print(len(n1))  #50
    plt.plot(bins1[:-1],n1,'--')
    plt.plot(bins2[:-1],n2,'--')

    多类型直方图

    import numpy as np
    import matplotlib.pyplot as plt
    
    # 生成3组值,每组的个数可以不一样
    x1,x2,x3 = [np.random.randn(n) for n in [10000, 5000, 2000]]
    
    plt.figure(figsize=(8,5))
    # 在 ax.hist 函数中先指定图例 label 名称
    plt.hist([x1, x2, x3], bins=10, density=True, histtype='bar')
    
    # 通过 ax.legend 函数来添加图例
    plt.legend(list("ABC"))
    
    plt.show()

    添加说明信息

    添加title

    你可以给图示或图添加标题。但是默认不支持中文,需要自己添加中文字体。

    windows的字体文件放在 C:WindowsFonts,通过fontproperties设置字体,fontsize设置字体大小.

    import matplotlib
    
    songTi = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')
    plt.title("多类型直方图",fontproperties=songTi,fontsize=12)

    添加文字、网格、轴标签及轴范围

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Fixing random state for reproducibility
    np.random.seed(19680801)
    
    mu, sigma = 100, 15
    x = mu + sigma * np.random.randn(10000)
    
    # the histogram of the data
    n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)
    
    
    plt.xlabel('Smarts')
    plt.ylabel('Probability')
    plt.title('Histogram of IQ')
    plt.text(60, .025, r'$mu=100, sigma=15$')  #(x,y,str,...)
    plt.xlim(40, 160)
    plt.ylim(0, 0.03)
    plt.grid(True)
    plt.show()

    参考链接:

    1. 知乎-matplotlib可视化直方图

    2. matplotlib.pyplot.hist官方文档

    3. matplotlib.pyplot.text官方文档及示例

    4. CSDN-Watch_dou-matplotlib之hist详解

  • 相关阅读:
    装饰器
    返回函数
    用Token令牌维护微服务之间的通信安全的实现
    用Windbg来分析.Net程序的dump
    Windows下docker的安装,将ASP.NET Core程序部署在Linux和Docker中
    StackExchange.Redis学习笔记(五) 发布和订阅
    StackExchange.Redis学习笔记(四) 事务控制和Batch批量操作
    StackExchange.Redis学习笔记(三) 数据库及密码配置 GetServer函数
    StackExchange.Redis学习笔记(二) Redis查询 五种数据类型的应用
    Task及Mvc的异步控制器 使用探索
  • 原文地址:https://www.cnblogs.com/lfri/p/12251387.html
Copyright © 2011-2022 走看看