Frage

I am having trouble maintain equal distance between tick marks on X axis while plotting a histogram. I specify the bins I want to use.

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

Now how can I make the space between the ticks 0,50,100..1000 same?

War es hilfreich?

Lösung

Replace last two lines by

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

This plots the bars at equal-spaced x positions (1, 2, 3, ...). It then adds the labels of your bin edges (which do not correspond to the actual x values, but no matter) and sets the x axis limits according to the values actually used for the x axis.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top