Create bar histogram with histc and patch

2 views (last 30 days)
franky
franky on 3 Aug 2018
Hello,
I have a vector with a measurement series which I splitted into multiple bins with histc. After that I calculated some special points like max, min, std, mean and error count for each bin with the accumarray function. Now I want to plot a histogram as bar plot and the other values as stand alone points.
The problem is that some bins are empty (zero) because the measurement values don't cover the entire bin range. Then groupTqReqL2Bins_max and groupTqReqL2Bins_min are also zero and my calculation for the bin scale does not work. My question is now: How can I calculate the bin scales for the patch function in these cases.
Here is my Code written with Matlab 2012b:
TqReqEdges = linspace(min(TqReqL2Sorted),max(TqReqL2Sorted)+200,TqReqBins);
[TqReqBinCount, TqReqID] = histc(TqReqL2Sorted, TqReqEdges);
groupTqReqL2_mean = accumarray(TqReqID,TqReqL2Sorted,[],@mean);
groupTqReqL2Bins_max = accumarray(TqReqID,TqReqL2Sorted,[],@max); % get start point for every bin
groupTqReqL2Bins_min = accumarray(TqReqID,TqReqL2Sorted,[],@min); % get end point for every bin
groupTqReqL1_mean = accumarray(TqReqID,TqReqL1Sorted,[],@mean);
groupTqReqL1_std = accumarray(TqReqID,TqReqL1Sorted,[],@std);
groupTqReqL1_max = accumarray(TqReqID,TqReqL1Sorted,[],@max); % get max deviation
groupTqReqL1_min = accumarray(TqReqID,TqReqL1Sorted,[],@min); % get min deviation
groupTqReqOutRange = accumarray(TqReqID,TqReqOutRange,[],@sum); % sum errors by group
% Get bin scales for both diagrams
diffTqReqBins=groupTqReqL2Bins_max-groupTqReqL2Bins_min; % bin length
TqReqBinsHisto=[groupTqReqOutRange diffTqReqBins]; % error frequency
TqReqBinsHistoL = [0; cumsum(diffTqReqBins)]; % error frequency cumulative
TqReqBinMiddle=diff(TqReqBinsHistoL)/2+TqReqBinsHistoL(1:end-1,1);
……
for k1 = 1:size(TqReqBinsHisto,1)
hl1 = patch([0 1 1 0]*TqReqBinsHisto(k1,2)+TqReqBinsHistoL(k1),...
[0 0 1 1]*TqReqBinsHisto(k1,1),...
BarColorArray(k1,:),... % use of alternating colours
'LineWidth',0.1,'Parent',ax1);
end
plot(ax2,TqReqBinMiddle,groupTqReqL1_mean,'x','MarkerEdgeColor',GreenDark,'MarkerSize',8,'LineWidth',2);
plot(TqReqBinMiddle,groupTqReqL1_max,'^','MarkerEdgeColor',GreenDark,'MarkerSize',8,'LineWidth',2,'Parent',ax2)
plot(TqReqBinMiddle,groupTqReqL1_min,'v','MarkerEdgeColor',GreenDark,'MarkerSize',8,'LineWidth',2,'Parent',ax2)
plot(TqReqL2Sorted,TqReqMaxSorted,'r','LineWidth',2,'Color',Red,'Parent',ax2)
plot(TqReqL2Sorted,TqReqMinSorted,'r','LineWidth',2,'Color',Red,'Parent',ax2)
……

Answers (0)

Products


Release

R2012b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!