Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Holding legend in boxplots

509 views
Skip to first unread message

Anbazhagan

unread,
Apr 12, 2010, 9:26:04 AM4/12/10
to
I am plotting two boxplots in one figure using hold on in matlab 7.1. Legend of the first boxplot gets lost when second one is plotted. How can i hold the legend as well? I

Walter Roberson

unread,
Apr 12, 2010, 2:27:57 PM4/12/10
to

Is it possible that the older legend is present but hidden underneath the
newer one? Did you specifically use different positions in the legend() function ?

Anbazhagan

unread,
Apr 13, 2010, 1:11:04 AM4/13/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <hpvonf$11g$2...@canopus.cc.umanitoba.ca>...

yes. it is hidden underneath. Am using the 'best' position for both legends.

Walter Roberson

unread,
Apr 13, 2010, 10:38:53 AM4/13/10
to

Well, change your code so that you use different specific corners.
legend() does not look at the graphics drawn already in order to find
the best place: it only looks at the data values of the axis it is
associated with.

Anbazhagan

unread,
Apr 13, 2010, 11:03:23 AM4/13/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <hq1vlt$ihc$2...@canopus.cc.umanitoba.ca>...

I tried your suggestion but the first legend flashes for a second and then disappears.
Then it displays only one legend but it is wrong i.e; it displays second boxplot legend text with the first boxplot line color.

I tried this approach but I receive an error
M={'Test A',Test B'};
p1=boxplot(bxplt_data2,'orientation','horizontal','label',bxplt_lbl,'colors','b','symbol','b+');
hold on
p2=boxplot(bxplt_data1,'orientation','horizontal','label',bxplt_lbl,'colors','r','symbol','r+');
legend([p1,p2],M);
*****************
Error:
??? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> legend at 198
elseif narg > 0 && ~ischar(varargin{1}) && ...
*****************
The above approach works well for the normal plots.
I dont know what I am doing wrong with boxplots.

Tom Lane

unread,
Apr 13, 2010, 1:37:31 PM4/13/10
to
> p1=boxplot(bxplt_data2,'orientation','horizontal','label',bxplt_lbl,'colors','b','symbol','b+');
> hold on
> p2=boxplot(bxplt_data1,'orientation','horizontal','label',bxplt_lbl,'colors','r','symbol','r+');
> legend([p1,p2],M);
> *****************
> Error:
> ??? Operands to the || and && operators must be convertible to logical
> scalar values.

One issue is that the two boxplot commands each return a matrix of handles
to different parts of the boxes. The legend function isn't expecting you to
pass in a matrix of handles.

You might find it easier to approach this way:

M={'Test A','Test B'};
X1 = randn(30,3);
X2 = 10+randn(30,3);
pos = [(1:size(X1,2)), (1:size(X2,2))];
p1=boxplot([X1 X2],'orientation','horizontal','label',{'#1' '#2' '#3' '' ''
''},...
'symbol','+','pos',pos,'color','rb');
legend(p1(1,[1 2]),M);

-- Tom


Anbazhagan

unread,
Apr 15, 2010, 5:44:04 AM4/15/10
to
This appraoch doesnt work either. I get two errors.
First I get an error message saying that 'Incorrect Number of labels'.
My labels have only thirty values. Matlab needs sixty values to plot. So instead of one '#1' label there are two '#1' labels.
I want the two boxplots to share the same labels which is not possible with this approach i guess.

"Tom Lane" <tl...@mathworks.com> wrote in message <hq2a4r$4vj$1...@fred.mathworks.com>...

0 new messages