直方图上的刻度线之间的相等距离

时间:2014-01-28 18:30:51

标签: matlab plot histogram axes

在绘制直方图时,我无法在X轴上保持刻度线之间的相等距离​​。我指定了我想要使用的垃圾箱。

X = [ 800 ; 700 ; 200 ; 50 ; 20 ; 20 ; 10] ;               Y = [1;1;1;1;1;1;1] ;

% The bins I want to use -->
BinEdges   = [0 10 50 100 500 1000] ; 


% Get the edge centers 
EdgeLowEach= BinEdges(1:end-1);     %# bins lower edge
EdgeUprEach= BinEdges(2:end);       %# bins upper edge
EdgeCentr  = (( EdgeLowEach + EdgeUprEach ) ./ 2)';      %# bins center   

[n,BinIdx] = histc(X, BinEdges) ;
Y          = accumarray(BinIdx,  Y, [], @sum);

h = figure; set(h,'name','Trial','numbertitle','off') ; hold on ;

bar(EdgeCentr,Y,'hist') ; 
set(gca, 'XTick',BinEdges, 'XLim',[BinEdges(1) BinEdges(end)])

enter image description here

现在我怎样才能在0,50,100..1000的刻度线之间留出相同的空间?

1 个答案:

答案 0 :(得分:2)

替换最后两行
bar(Y,'hist'); %// this uses 1:numel(Y) as x axis values
set(gca,'XTick',.5:numel(Y)+.5,'Xticklabel',BinEdges,'XLim',[.5 numel(Y)+.5])

enter image description here

这绘制了等间距x位置(1,2,3,...)处的条形图。然后它会添加bin边缘的标签(与实际的x值不对应,但无关紧要),并根据实际用于x轴的值设置x轴限制。