直方图 又回到了校园,空气是如此的新鲜,人是如此的少...... 在Python里,直方图的绘制方法不止一种,先介绍常用的一种。Matplotlib这个包是Python中比较强大的绘图的包,可以绘制包括饼图、条形图、散点图、折线图、面积图、三维图在内的等等等等二维三维图形,等有时间,我再慢慢整理发布。在matplotlib.pyplot中是用hist()函数来绘制直方图。绘制直方图时,首先要有数据,hist()函数会自动根据数据和柱子的数目去数每个柱子应该有多少数据,并绘制成图形。 import matplotlib.pyplot as plt#要使用matplotlib来绘制,必须先导入此包x=np.random.randint(0,100,100)#生成【0-100】之间的100个数据plt.hist(x,bins=10)#x为要统计的数据,bins表示有多少条柱子hist(x,bins=None,range=None, density=None, bottom=None, histtype='bar',, log=False, color=None, label=None, stacked=False, normed=None)x: 数据集,最终的直方图将对数据集进行统计bins: 统计的区间分布,即要显示几条柱子range: tuple, 显示的区间,range在没有给出bins时生效density: bool,是否归一化,若为True则归一化显示histtype: 可选{'bar', 'barstacked', 'step', 'stepfilled'}之一,默认为bar柱形,align: 可选{'left', 'mid', 'right'}之一,默认为'mid',控制柱状图的水平分布,left或者right,会有部分空白区域,推荐使用默认log: bool,默认False,即y坐标轴是否选择指数刻度stacked: bool,默认为False,是否为堆积状图--------------------------------代码太多,休息一下
--------------------------------我是广告开始的分隔线 --------------------------------我是广告结束的分隔线 --------------------------------接着,来...  import matplotlib.pyplot as pltx=np.random.randint(0,100,100)plt.hist(x,bins=10,color='r',density=True,histtype='bar',rwidth=0.5,align='right')y=np.random.randint(0,100,100)plt.hist(y,bins=10,color='b',density=True,histtype='bar',rwidth=0.5)
 import matplotlib.pyplot as pltx=np.random.randint(0,100,100)n1,bins1,patches1=plt.hist(x,bins=10,color='r',density=True,histtype='bar',rwidth=0.5,align='right')n2,bins2,patches2=plt.hist(x,bins=10,color='b',density=True,histtype='bar',rwidth=0.5)y=np.random.randint(0,100,100)n3,bins3,patches3=plt.hist(y,bins=10,color='g',density=True,histtype='bar',rwidth=0.5)我这种判断方法感觉有点傻,网上有其他的判断直方图相似性等的函数,有感兴趣的朋友请自行搜索。
2、cv.calcHist()也可以画直方图。将在下一篇中介绍。
|