Creating customized barplot for different lengths of arrays
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have data comes from three different sources. Each of [data1, data2, and data3] are column vectors with different lengths and it only contains integer values from 1-3. Like following:
data1 = randi([1 3], 20,1])
data2 = randi([1 3], 34,1])
data3 = randi([1 3], 56,1])
The code should calculate the percentage of each integers value and group them on the same bar plot for data1, data2, and data 3. See below hand sketched picture [approximate]. Any thoughts would be greatly appreciated!
1 Kommentar
dpb
am 30 Dez. 2016
BTW: As discussed in the other comment and the answer, that it takes such effort in HG1 and is, from what I've read at File Exchange, even more complicated if not actually impossible with HG2 is simply unacceptable in my opinion.
I would suggest submitting this topic as a bug report; it really is a worse omission than simply an enhancement request.
Just imo, $0.02, ymmv, etc., etc., with all the other associated caveats...
Akzeptierte Antwort
dpb
am 29 Dez. 2016
Bearbeitet: dpb
am 30 Dez. 2016
n=100*bsxfun(@rdivide,[histc(data1,1:3) histc(data2,1:3) histc(data3,1:3)], ...
[length(data1) length(data2) length(data3)]).';
hBarH=bar(n,'hist'); % force style to return array handle to patches
xpts=cell2mat(get(hBarH,'XData')); % return patch x position array
xpts=mean(reshape(xpts(1:2:end),2,[])) % then average by twos...
set(gca,'xtick',xpts,'xticklabel',[1:3]) % set new tick positions, label
ylim([0 50])
y=n.'; y=y(:); % order from left to right in column vector to label
hTxt=text(xpts,y(:),num2str(y(:),'%.1f%%'), ...
'horizontalalign','center', ...
'verticalalign','bottom', ...
'fontsize',8);
to get
NB: See the more extensive discussion in Comment about the logic behind the above positioning and caveats re: HG vis a vis HG2 about what you can/cannot possibly be able to do with the latter.
NB2: The default 'group' option, while I can't tell the result apart from 'hist' visually, results in bar returning an array of hggroup objects, the patch objects of which are children. This makes retrieving their 'XData' property another level of indirection; hence I force the 'hist' style to get the vector of patch objects directly instead.
ADDENDUM
I just noticed the trees for the forest--have always concentrated on how to retrieve the necessary data so didn't pick up on what looks to be a simple algorithm to compute the bar positions. It looks from this sample of one that the delta to the various bars from the center x position is (N-1)/(M*N) where N is number of bars/group and M is number of groups.
That would be readily computed without handle-diving if it holds in general--
>> dx=(size(n,1)-1)/numel(n);
>> xpts=[[1:3].'-dx [1:3].' [1:3].'+dx].'; xpts=xpts(:).'
xpts =
0.7778 1.0000 1.2222 1.7778 2.0000 2.2222 2.7778 3.0000 3.2222
>>
Those are the values we obtained from the averages of the patches x-axis locations above so looks like we've uncovered the magic Rosetta stone used by the internal logic. Try this out with HG2; if it works as I suspect it will, then the problem about opaque objects at least has a relatively simple workaround for this particular issue.
A NOTE: I tried some other sizes of groups and bars/group and while the above gets in the ballpark, the spacings aren't that simply derived for them so the above is true only for the case of the 3x3 arrangement it appears, unfortunately.
6 Kommentare
dpb
am 1 Jan. 2017
"...finding midpoints was very thoughtful from you..."
Well, I'd had occasion to do the labeling before so had managed previously to get there so I had a starting point... :) Hadn't previously got it down to quite as concise as the reasonably simple calculation above, but knew the general idea and so another iteration made some simplification easier. Anyway, thanks for the kind words and glad it helped.
With HG1 (prior to R2014b) this method should always work with the aforementioned caveat about hggroup vis a vis bar patch handles depending on the option chosen.
I believe I will submit a subset of the question (again) as a bug this time rather than an enhancement since now with HG2 they've broken any possible way other than having to reverse engineer the spacing algorithm that's buried in inaccessible code below the m-file level (I've already looked :<).
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!