Boxplot with multiline x axis labels

Hello,
I'm trying to create a boxplot for two groups. Each group's label is multi line. I have tried the approach below, but the first line of each label is drawn on top of the x axis, while the second is drawn below the x axis (please try the code below)
Does anyone know how to make the two lines be drawn below the x axis in a boxplot?
Thanks
%%%%%%%%%%%
Dist1 = rand(10, 1);
Dist2 = rand(10, 1);
group = [repmat(cellstr(sprintf('First Line G1\nSecond Line G1')), length(Dist1), 1); ...
repmat(cellstr(sprintf('First Line G2\nSecond Line G2')), length(Dist2), 1)];
figurePlot = figure('visible', 'on');
boxplot([Dist1;Dist2], group)
ylabel('Y Label');

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 10월 4일

0 개 추천

After your plot, use findobj() to find the handles of the two labels and then change the position.
h=findobj(gca,'type','text');
char(get(h,'String'))
get(h,'Position')
ans =
First Line G2
Second Line G2
First Line G1
Second Line G1
ans =
[1x3 double]
[1x3 double]

댓글 수: 6

Mark
Mark 2011년 10월 4일
Thanks but then how do you set the new position? I tried the following but it does not work:
Dist1 = rand(10, 1);
Dist2 = rand(10, 1);
group = [repmat(cellstr(sprintf('First Line G1\nSecond Line G1')), length(Dist1), 1); ...
repmat(cellstr(sprintf('First Line G2\nSecond Line G2')), length(Dist2), 1)];
figurePlot = figure('visible', 'on');
boxplot([Dist1;Dist2], group)
ylabel('Y Label');
h=findobj(gca,'type','text');
posText = get(h,'Position')
offset = [0 10 0];
set(h,'Position', [posText{1, :} - offset;posText{2, :} - offset]);
Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
You can't use set() like that. It has to be done one by one.
h=findobj(gca,'type','text');
set(h(1),'Position',get(h(1),'Position')-10);
set(h(2),'Position',get(h(2),'Position')-10);
Mark
Mark 2011년 10월 4일
Thanks so much Fangjun. That worked as I wanted. I owe you a beer :-)
Unfortunately, when I try to either print or save the figure, the axis labels go back to their original position. For instance, you can try this and see:
strFName = 'c:\someFig.png';
saveas(figurePlot, strFName);
The image is saved with the labels in the original place.
Any clue as to how to avoid this?
Thanks in advance.
Walter Roberson
Walter Roberson 2011년 10월 4일
The print() operation involves a resize callback on the figure to size it to the paper size. Setting the figure PaperPositionMode to manual can help that, as can providing your own figure resize function. What you are doing, though, is inherently a bit weak in the face of resizes.
What I would suggest is that instead of working with \n, that you instead pass in a LaTex or Tex command for each group that encodes the linebreak within it. Then, right after the boxplot(),
set(findobj(gca,'-depth',1,'Type','text'),'Interpreter','latex')
(or tex if you prefer that.)
Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
I had the same problem using saveas() or print(). Walter's suggestion is probably the best option. However, I can't test it at this moment.
Fangjun Jiang
Fangjun Jiang 2011년 10월 5일
I used Tex formatting but it still shows the first line label above the x-axis. So it still requires changing the position to move it down. Then when using saveas(), the label still moves up. Please post another question using the following code, there might be experts on graphics who can help you.
Dist1 = rand(10, 1);
Dist2 = rand(10, 1);
group = [repmat({'First Line G1\newlineSecond Line G1'}, length(Dist1), 1); ...
repmat({'First Line G2\newlineSecond Line G2'}, length(Dist2), 1)];
figurePlot = figure('visible', 'on');
boxplot([Dist1;Dist2], group)
ylabel('Y Label'); grid;
h=findobj(gca,'type','text');
set(h,'Interpreter','tex')
set(h(1),'Position',get(h(1),'Position')-10);
set(h(2),'Position',get(h(2),'Position')-10);
saveas(figurePlot,'test.png');

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 10월 4일

0 개 추천

Using string with newlines in them is not promised to work for any of the MATLAB graphical text operations that I can think of at the moment.
Some graphical operations permit character arrays with multiple rows; some graphical operations permit strings with the lines separated by '|'; some graphical operations permit cell arrays of strings; some permit TeX or LaTeX that include coded indications of line breaks. And some graphical text operations just plain only permit single lines. Sometimes the easiest way to work around the matter is to go in to the object (e.g., axes) properties and bash the usual text object handle to be an annotation object handle.

댓글 수: 4

Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
In this case, the OP is not using newline. cellstr() is used. However, I found that in R2007b, it is displayed in one line. In R2010b, it is displayed in two lines.
Walter Roberson
Walter Roberson 2011년 10월 4일
cellstr() does not break the strings at newline characters. A "row" for cellstr purposes is the same thing as a MATLAB array row.
Walter Roberson
Walter Roberson 2011년 10월 4일
Did you perhaps miss the sprintf('....\n...') ? That creates a character vector with the \n replaced by char(10) and does *not* create two rows of characters.
Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
Yep. I see you point.
>> a=['abc';'efg'];
A=cellstr(a)
b=sprintf('abc\nefg');
B=cellstr(b)
A =
'abc'
'efg'
B =
[1x7 char]

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

카테고리

도움말 센터File Exchange에서 Labels and Annotations에 대해 자세히 알아보기

질문:

2011년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by