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

putting legend outside the plot

412 views
Skip to first unread message

Similione Kasan

unread,
Feb 27, 2010, 9:17:05 AM2/27/10
to
In gplotmatrix.m, following is the code for putting legend on top right.

% Create legend if requested; base it on the top right plot
if (doleg)
gn = gn(ismember(1:size(gn,1),g),:);
legend(ax(1,cols),gn);
end

I want to put this legend on top right but outside the plot. I have seen in help that there is a command "NorthEastOutside" for putting it out of the plot. But I dont know how to include this option in the code given above. Can anyone help me plz?

us

unread,
Feb 27, 2010, 9:28:04 AM2/27/10
to
"Similione Kasan" <mech...@hotmail.com> wrote in message <hmb9h1$ji7$1...@fred.mathworks.com>...

one of the solutions

% create your plot
lh=findall(gcf,'tag','legend');
set(lh,'location','northeastoutside');
% but: you'll see that it screws up the plot...

us

Similione Kasan

unread,
Feb 27, 2010, 9:49:04 AM2/27/10
to
"us " <u...@neurol.unizh.ch> wrote in message <hmba5k$a8$1...@fred.mathworks.com>...


Thanks for the reply but I remember one of the guys introduce a new variable say "h" in the same code I mentioned above and for that variable h he made the legend to appear outside the plot......that did not scew the plot but the changings he made are on his computer.....

Could you think how would he have done that.

us

unread,
Feb 27, 2010, 10:30:23 AM2/27/10
to
"Similione Kasan"

> Thanks for the reply but I remember one of the guys introduce a new variable say "h" in the same code I mentioned above and for that variable h he made the legend to appear outside the plot......that did not scew the plot but the changings he made are on his computer.....
>
> Could you think how would he have done that.

well, yes...

ph=plot(rand(10,1),'-ks');
ah=axes('position',[.1,.1,.5,.5],'visible','off');
lh=legend('v6',ah,ph,'foo','location','northeastoutside');
% but: you'll see the runtime warning (r2009b)...

% it would probably be easier to set the LEGEND's position prop
% your plot...


lh=findall(gcf,'tag','legend');

lp=get(lh,'position');
set(lh,'position',[.1,.1,lp(3:4)]);

us

Similione Kasan

unread,
Feb 27, 2010, 10:44:04 AM2/27/10
to
"us " <u...@neurol.unizh.ch> wrote in message <hmbdqf$ko3$1...@fred.mathworks.com>...

----I have tried this but still I have to set the position which takes sometime. I dont say that it is wrong but I am sure my friend just changed one little thing in the following code and automatically legend started to appear outside the graph on top right position and the symmetry of graph was not disturbed....He did not add anything special instead introduced "NorthEastOutside" somewhere in the code given below:

Similione Kasan

unread,
Feb 27, 2010, 10:50:21 AM2/27/10
to
"Similione Kasan" <mech...@hotmail.com> wrote in message <hmbek4$avd$1...@fred.mathworks.com>...


---------------------

Hey I have found the way to put it outside...see the changes I made to the code:

% Create legend if requested; base it on the top right plot
if (doleg)
gn = gn(ismember(1:size(gn,1),g),:);

h=legend(ax(1,cols),gn);
set(h,'Location','NorthEastOutside');

end

I simply set h as the legend and wrote it to be outside the plot.....
But anyways thanksalot for taking interest in my post :)) Keep up the good work "us"....

us

unread,
Feb 27, 2010, 11:07:04 AM2/27/10
to
"Similione Kasan"

> % Create legend if requested; base it on the top right plot
> if (doleg)
> gn = gn(ismember(1:size(gn,1),g),:);
> h=legend(ax(1,cols),gn);
> set(h,'Location','NorthEastOutside');
> end
>
> I simply set h as the legend and wrote it to be outside the plot.....
> But anyways thanksalot for taking interest in my post :)) Keep up the good work "us"....

well... this mimics EXACTLY what i told you in my first reply(?)...

lh=findall(gcf,'tag','legend'); % <- instead of returning the handle H...
set(lh,'location','northeastoutside');

% now, since you seem to be happy with the solution(?) - and have actually
% changed the function(!), why not simply add

legend(ax(1,cols),gn,'location','northeastoutside');

us

Similione Kasan

unread,
Feb 27, 2010, 12:36:03 PM2/27/10
to
"us " <u...@neurol.unizh.ch> wrote in message <hmbfv7$69n$1...@fred.mathworks.com>...

----------
oh yes you are correct...I just realized that...thanks for ur time

0 new messages