problem with x-axis labels after overlapping 2 box plots

조회 수: 10 (최근 30일)
Dawn
Dawn 2014년 5월 30일
댓글: Kelly Kearney 2014년 6월 2일
I have the following code to compare two sets of box plots and there seems to be two misaligned x-axis labels after doing that! I have no idea what is causing this strange misalignment. Could the use of 'hold' be a problem in this case? Im using matlab 2014. Thanks.
x = rand(5,1);
y = rand(10,1);
z = rand(15,1);
Xpos = [2 5 12];
group = [repmat(Xpos(1), 5, 1); repmat(Xpos(2), 10, 1); repmat(Xpos(3), 15, 1)];
data=[x;y;z];
figure
boxplot(data, group,'positions', Xpos,'colors','k')
hold on
x =2+ rand(5,1);
y = 2+rand(10,1);
z = 2+rand(15,1);
group = [repmat(Xpos(1), 5, 1); repmat(Xpos(2), 10, 1); repmat(Xpos(3), 15, 1)];
data=[x;y;z];
boxplot(data, group,'positions', Xpos,'colors','r')
ylim([0 4])
  댓글 수: 4
Star Strider
Star Strider 2014년 5월 30일
I can’t reproduce it in R2014a. Looks fine — no duplicated or repeated x values.
To wit:
Although it doesn’t match Dawn’s plot. There may be something strange in Dawn’s workspace (workspace variables?) or in her code that we are not privy to.
The real Question is what Dawn did to get MATLAB mad at her?
Dawn
Dawn 2014년 6월 2일
편집: Dawn 2014년 6월 2일
On first sight it may look fine but if I try to resize the plot by manually expanding it, the misalignment problem will show up. It happens all the time, even after clearing the workspace and pasting just the exact code that I posted above.
Would appreciate if the kind souls out there could let me know if to same thing happens to you after you resize your plots.

댓글을 달려면 로그인하십시오.

채택된 답변

Kelly Kearney
Kelly Kearney 2014년 6월 2일
편집: Kelly Kearney 2014년 6월 2일
The answer to the "why" is that boxplot, for reasons I've never been able to figure out, doesn't just change the tick positions and labels. Instead, it removes the xticks completely (or y-ticks, in the case of a horizontally-oriented boxplot) and replaces them with text annotation objects. And annotation objects are positioned relative to the figure, not the axis. Apparently slightly differently each time (possibly due to the fact that boxplot also, for reasons unknown, resizes axis, hence changing the figure-to-axis ratios between calls).
The easiest fix:
delete(findall(gcf, 'type', 'text'));
set(gca, 'xticklabelmode', 'auto', 'xtickmode', 'auto');
  댓글 수: 3
dpb
dpb 2014년 6월 2일
...boxplot, for reasons [unknown], doesn't just change the tick positions and labels. Instead, it removes the xticks ... and replaces them with text annotation objects
And how bizzaro!!! :( Seems worthy of bug report fer shure...at least an enhancement request to rid it of such foolishness. Who would ever think of doing such a thing????
Kelly Kearney
Kelly Kearney 2014년 6월 2일
I think the original intent was to allow vertically-rotated tick labels (which is the default with the 'compact' option), but it's a pretty clunky implementation in my opinion.
I use boxplots quite a lot, and got really fed up with the heavy-handed axis changes, to the point that I wrote my own replacement:
Though my version handles multiple datasets a bit differently, and doesn't accept the Group input option.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by