Matlab中hist、 histc、bar函数的用法及区别:
首先给出matlab帮助文件的介绍:
Histc
Syntax
1.bincounts = histc(x,binranges)
2.bincounts = histc(x,binranges,dim)
3.[bincounts,ind]= histc(___)
Description
bincounts= histc(x,binranges) counts the number of values in x that are within each specified bin range. Theinput, binranges, determines the endpoints for each bin.The output, bincounts, contains the number of elements from x in each bin.
· If x is a vector, then histc returns bincounts as a vector of histogram bin counts.
· If x is a matrix, then histc operates along each column of x and returns bincounts as a matrix of histogram bin counts for eachcolumn.
计算x中的值在每一个箱子(bin)中的个数,binranges决定了每个箱子的端点,也就是划分区间的端点。如果x是向量,那么返回一个向量各个区间有多少个数落在此区间的向量。如果x是矩阵,则对x的每一列进行计算,并返回一个矩阵,对应每一列中位于相应区间的值的个数。因此,如果是想计算整个矩阵中位于某一区间的个数,则把输出矩阵行相加即可。
To plot the histogram, use bar(binranges,bincounts,'histc').
bincounts =histc(x,binranges,dim) operates along the dimension dim.
比上一个多了一个参数‘dim’,指定处理的方向,即1是按列处理,2是按行处理。
[bincounts,ind]=histc(___) returns ind, an array the same size as x indicating the bin number that each entry in x sorts into. Use this syntax with any of theprevious input argument combinations.
这个用法多出来个‘ind’输出参数,该参数指明x的值落入了那个箱子(箱子号),其长度与x的产生过度一致。
那么问题来了,histc的箱子是怎么划分区间的,就是那边是闭区间,那边是开区间。测试:
>> range=1:5
range = 1 2 3 4 5
>> aa
aa = 1 2 2 2 3 4 4 5 5 5 5 5 5
>> [i1,i2]=histc(aa,range)
i1 = 1 3 1 2 6
i2 =1 2 2 2 3 4 4 5 5 5 5 5 5
可以发现histc的5区间(箱子)的划分为
[1,2),[2,3),[3,4),[4,5),[5,inf)
Syntax
· hist(data)example
· hist(data,nbins)example
· hist(data,xvalues)example
· hist(axes_handle,___)example
· nelements= hist(___)
· [nelements,centers]= hist(___)example
Description
hist(data) creates a histogram bar plot of data. Elements in data are sorted into 10 equally spaced
bins along the x-axis between the minimum and maximum values ofdata. Bins are displayed as rectangles such that the heightof each rectangle indicates the number of elements in the bin.
等间隔划分对max(data)- min(data)的值进行10等间隔划分并作图
hist(data,nbins) sorts data into the number of bins specified by the scalar nbins.
对data进行制定nbins的区间进行等间隔划分并作图。
hist(data,xvalues) sorts data into bins with intervals determined by the vector xvalues.
· If xvalues is a vector of evenly spaced values,then hist uses the values as the bin centers.
· If xvalues is a vector unevenly spaced values,then hist uses the midpoints betweenconsecutive values as the bin edges.
The length of the vector, length(xvalues), determines the number of bins.
如果xvalues是均匀的,则hist使用xvalues值作为边界的中心
如果xvalues 是非均匀的,则hist使用连续的边界值的中值作为中心
hist(axes_handle,___) plots into the axes specified by axes_handle instead of into the current axes (gca). The option axes_handle can precede any of the input argument combinationsin the previous syntaxes.
nelements =hist(___) returns a row vector, nelements, indicating the number of elements in each bin.
返回一个行矢量,矢量中的每一个值指明了每个区间中的值的个数
[nelements,centers]= hist(___) returns an additional row vector, centers, indicating the location of each bincenter on the x-axis. To plot the histogram, youcan usebar(centers,nelements).
‘centers’指明了每个区间的中值
bar
Syntax
· bar(Y)example
· bar(x,Y)example
· bar(___,width)example
· bar(___,style)example
· bar(___,bar_color)example
· bar(___,Name,Value)example
· bar(axes_handle,___)example
· h = bar(___)
Description
bar(Y) draws one bar for each element in Y.
绘制Y中的每一个元素在一个柱中
y = [75.995,91.972,105.711,123.203,131.669,...
150.697,179.323,203.212,226.505,249.633,281.422];
figure;
bar(y)
bar(x,Y) draws bars for each column in Y at locations specified in x.
‘x’指定了绘制Y的每一列数据的位置
bar(___,width) sets the relative bar width andcontrols the separation of bars within a group and can include any of the inputarguments in previous syntaxes.
定义了每个bar的宽度
bar(___,style) specifies the style of the bars andcan include any of the input arguments in previous syntaxes.
定义bar的样式
Y = round(rand(5,3)*10);
figure;
subplot(2,2,1);
bar(Y,'grouped');
title('Group')
subplot(2,2,2);
bar(Y,'stacked');
title('Stack')
subplot(2,2,3);
bar(Y,'histc');
title('Histc')
subplot(2,2,4);
bar(Y,'hist');
title('Hist')
bar(___,bar_color) displays all bars using the colorspecified by the single-letter abbreviation of bar_color and can include any of the input arguments inprevious syntaxes.
定义了bar图形的填充色
y = [75.995,91.972,105.711,123.203,131.669,...
150.697,179.323,203.212,226.505,249.633,281.422];
figure;
bar(y,'r');
bar(___,Name,Value) sets the property names to thespecified values and can include any of the input arguments in previoussyntaxes.
设定属性名为‘Name’的‘Value’
Note: You cannot specify names and values when using hist or histc options. |
y = [75.995,91.972,105.711,123.203,131.669,...
150.697,179.323,203.212,226.505,249.633,281.422];
figure;
bar(y,'g','EdgeColor',[1,0.5,0.5]);
bar(axes_handle,___) plots into the axes with the handle axes_handle instead of into the current axes (gca).
把图绘制到指定的句柄图形