今天学习直方图绘制,使用了hist函数绘制直方图。
首先看官网定义:
matplotlib.pyplot. hist (x, bins=None, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, 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. |
这里先解释直方图(histogram)是什么?
直方图是为了表明数据分布情况。通俗地说就是哪一块数据所占比例或者出现次数较高,哪一块出现概率低。如下图 横轴是数据,纵轴是出现的次数(也就是频数)。从这个图看4.1-4.3这块数据出现次数最高。 |
从上面可以看出直方图能够反映数据的分布状况。
在matplotlib中,我们使用hist函数完成直方图的绘制。(这里仅仅介绍最简单的使用方法,更多扩展请官网查询)
首先构造数据,这里注意构造的是一维数组可以使用pandas中的Series,如果是二维数组使用DataFrame。
运行结果(左边是数据,右边是频数,按照数据的大小来排序) 1 55 2 49 3 51 4 42 5 51 6 38 7 44 8 55 9 41 10 56 11 45 12 43 13 51 14 54 15 46 16 53 17 56 18 52 19 62 20 56 Name: 1, dtype: int64 随后开始画直方图:
运行结果 默认情况下,总共分为10段,可以数一下上面的段数。如果使用如下代码:
运行结果: 可以看出效果更明显。根据自己的需要进行选择!! |