histogram

A histogram is an accurate representation of the distribution of numerical data. It is an estimate of the probability distribution of a continuous variable (quantitative variable) and was first introduced by Karl Pearson.To construct a histogram, the first step is to "bin" (or "bucket") the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval. The bins are usually specified as consecutive, non-overlapping intervals of a variable. The bins (intervals) must be adjacent, and are often (but are not required to be) of equal size.

matplotlib.pyplot.hist

matplotlib.pyplot.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, hold=None, data=None, ***kwargs*)

Plot a histogram.

Compute and draw the histogram of x. The return value is a tuple (n, bins, patches) or ([n0, n1, …], bins, [patches0, patches1,…]) if the input contains multiple data.

Multiple data can be provided via x as a list of datasets of potentially different length ([x0, x1, …]), or as a 2-D ndarray in which each column is a dataset. Note that the ndarray form is transposed relative to the list form.

Masked arrays are not supported at present.

parameters

x : (n,) array or sequence of (n,) arrays

Input values, this takes either a single array or a sequence of arrays which are not required to be of the same length.

bins : integer or sequence or ‘auto’, optional

bins 即是 根据x中的数据集 划分 合适的组数。一般可以先用'auto',然后在此基础上对bins进行微调。

​ If an integer is given, bins + 1 bin edges are calculated and returned, consistent with numpy.histogram().

​ If bins is a sequence, gives bin edges, including left edge of first bin and right edge of last bin. In this case, bins is returned unmodified.

​ All but the last (righthand-most) bin is half-open. In other words, if bins is:

[1, 2, 3, 4]

​ then the first bin is [1, 2) (including 1, but excluding 2) and the second [2, 3). The last bin, however, is [3, 4], which includes 4.

​ Unequally spaced bins are supported if bins is a sequence.

​ If Numpy 1.11 is installed, may also be 'auto'.

​ Default is taken from the rcParam hist.bins.

density : boolean, optional

​ If True, the first element of the return tuple will be the counts normalized to form a probability density, i.e., the area (or integral) under the histogram will sum to 1. This is achieved by dividing the count by the number of observations times the bin width and not dividing by the total number of observations. If stacked is also True, the sum of the histograms is normalized to 1.

​ Default is None for both normed and density. If either is set, then that value will be used. If neither are set, then the args will be treated as False.

​ If both density and normed are set an error is raised.

returns

n : array or list of arrays

​ The values of the histogram bins. See normed or density and weights for a description of the possible semantics. If input x is an array, then this is an array of length nbins. If input is a sequence arrays [data1, data2,..], then this is a list of arrays with the values of the histograms for each of the arrays in the same order.

​ 默认,n 返回 落在每个区间里的数 的频数 的list;若指定density = True,n 返回 每个区间的概率密度值的列表

bins : array

​ The edges of the bins. Length nbins + 1 (nbins left edges and right edge of last bin). Always a single array even when multiple data sets are passed in.

patches : list or list of lists

​ Silent list of individual patches used to create the histogram or list of such list if multiple input datasets.

例子

ex1

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
############################
#File Name: hist.py
#Brief:
#Author: frank
#Mail: frank0903@aliyun.com
#Created Time:2018-06-13 22:03:35
############################ import matplotlib.pyplot as plt
import numpy as np a = [34, 40, 37, 30, 44, 36, 32, 26, 32, 36]
n,bins,patches = plt.hist(a,bins='auto')
print("n:{}, bins:{},pathes:{}".format(n,bins,patches))
plt.show()

从上例可知,bins 区间的个数为5个,即

[26,29.6], 落在 [26,29.6] 里的数是26, 频数是1

[29.6,33.2],落在[29.6,33.2]里的数是 30,32,32,频数是3

[33.2,36.8],落在[33.2,36.8]里的数是 34,36,36,频数是3

[36.8,40.4],落在[36.8,40.4]里的数是 37,40,频数是2

[40.4,44],落在[40.4,44]里的数是44,频数是1

ex2

看density参数对直方图的影响

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
############################
#File Name: hist.py
#Brief:
#Author: frank
#Mail: frank0903@aliyun.com
#Created Time:2018-06-13 22:03:35
############################ import matplotlib.pyplot as plt
import numpy as np a = [34, 40, 37, 30, 44, 36, 32, 26, 32, 36]
n,bins,patches = plt.hist(a,bins='auto',density=True)
print("n:{}, bins:{},pathes:{}".format(n,bins,patches))
plt.show()

从上例可知,当density为True时,直方图的y轴表示的是概率密度值。

\(\text{the bin width}=\frac {max-min}{bins}=\frac{44-26}{5}=3.6\)

[26,29.6], 落在 [26,29.6] 里的数是26, 频数是1,\(\frac {频数}{\text{the number of observations} \cdot \text{the bin width}}=\frac {1}{10\cdot 3.6}=0.02777778\)

其他区间的类似

python之histogram的更多相关文章

  1. Prometheus学习系列(三)之Prometheus 概念:数据模型、metric类型、任务、实例

    前言 本文来自Prometheus官网手册1.Prometheus官网手册2 和 Prometheus简介 说明 Prometheus从根本上存储的所有数据都是时间序列: 具有时间戳的数据流只属于单个 ...

  2. 灰度图的直方图均衡化(Histogram Equalization)原理与 Python 实现

    原理 直方图均衡化是一种通过使用图像直方图,调整对比度的图像处理方法:通过对图像的强度(intensity)进行某种非线性变换,使得变换后的图像直方图为近似均匀分布,从而,达到提高图像对比度和增强图片 ...

  3. python绘制图的度分布柱状图, draw graph degree histogram with Python

    图的度数分布 import collections import matplotlib.pyplot as plt import networkx as nx G = nx.gnp_random_gr ...

  4. [LeetCode]题解(python):084-Largest Rectangle in Histogram

    题目来源: https://leetcode.com/problems/largest-rectangle-in-histogram/ 题意分析: 给定一个数组,数组的数字代表这个位置上的bar的高度 ...

  5. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  6. opencv python:图像直方图 histogram

    直接用matplotlib画出直方图 def plot_demo(image): plt.hist(image.ravel(), 256, [0, 256]) # image.ravel()将图像展开 ...

  7. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  8. 1 python大数据挖掘系列之基础知识入门

    preface Python在大数据行业非常火爆近两年,as a pythonic,所以也得涉足下大数据分析,下面就聊聊它们. Python数据分析与挖掘技术概述 所谓数据分析,即对已知的数据进行分析 ...

  9. Python绘图

    1.二维绘图 a. 一维数据集 用 Numpy ndarray 作为数据传入 ply 1. import numpy as np import matplotlib as mpl import mat ...

随机推荐

  1. 通配置文件的方式控制java.util.logging.Logger日志输出

    转自:http://zochen.iteye.com/blog/616151 简单的实现了下利用JDK中类java.util.logging.Logger来记录日志.主要在于仿照log4j方式用配置文 ...

  2. SDK里报错[NSConcreteMutableData wbsdk_base64EncodedString]

    百度一大堆都说在这个里加个-ObjC,然后加了还是有问题 最近谷歌了下才要加入这个才正常了,国内的开发者只是一知半解的………… 如果错误还没有解决, 下面这个可以帮到你:

  3. Spring Dataflow批处理框架在OCP上的部署

    详细参考 https://donovanmuller.blog/spring-cloud-dataflow-server-openshift/docs/1.2.1.RELEASE/reference/ ...

  4. win7注册表损坏的修复方法

    win7注册表损坏的修复方法 发布时间:2013-07-19 09:31发布者:系统城-小薇浏览数:3129 注册表是window系统中的一个非常重要的数据库,用于存储电脑系统和应用程序的设置信息,我 ...

  5. python2和python3的编码问题

    python2中有两种类型 str字符串和unicode字符串 python3则改成了 bytes和str字符串 在python2中‘xxx’和b‘xxx’都是str字符串,u‘xxx’是unicod ...

  6. 关于json对象的删除

    摘自:http://xosadan.iteye.com/blog/1100383 关于json对象的删除 一个json对象在后台产生了,但是有些数据可能无效或者不合法,所以需要在前台作些例外处理,比如 ...

  7. Windows内存管理

    本博文很大程度上参考了,潘爱民先生的<Windows内核原理与实现>一书,在此对他表示感谢. 记得是在学C语言指针的时候,首次比较实际的使用内存寻址.也是在那个时候知道不能使用未初始化的指 ...

  8. [服务器安全]升级OpenSSH,OpenSSL,vsftp,关闭NTP服务

    公司的旧版直播服务器使用的是CentOS 6.7,很多软件包都是几年前的了.最近很多安全相关的新闻充斥着IT圈,先是Intel芯片有重大安全漏洞,后面MacOS爆安全漏洞.所以,对于安全问题还真不能小 ...

  9. Android中的线程池概述

    线程池 Android里面,耗时的网络操作,都会开子线程,在程序里面直接开过多的线程会消耗过多的资源,在众多的开源框架中也总能看到线程池的踪影,所以线程池是必须要会把握的一个知识点; 线程运行机制 开 ...

  10. 最小公倍数 【杭电-HDOJ-1108】 附题+具体解释

    /* 最小公倍数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...