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

Long Horizontal Wrapped Legend?

6,178 views
Skip to first unread message

JD

unread,
Aug 9, 2005, 10:41:02 AM8/9/05
to
Hey folks!

I need some help. I know my elbow from a hole in the ground when it
comes to MATLAB, but I cannot figure this one out...

I have a simple plot with 40 trends (vectors, variables, whatever you
want to call them). I want to put a horizontal oriented legend in
the 'West' position. (Using 7.0.1) However, the number of trends is
large and runs off the plot. I would like to wrap the legend so that
it is four lines:
-01 -02 -03 -04 -05 -06 -07 -08 -09 -10
-11 -12 -13 -14 -15 -16 -17 -18 -19 -20
.
.
.
... -40

Anybody know how to do this? I tried modifying legend.m so that it
does not delete the previous legend so I could make multiple calls to
legend for each line. I am close, but I was wondering if there is a
simpler way to do this!??

THANK YOU!!
JD

François Bouffard

unread,
Aug 9, 2005, 11:30:29 AM8/9/05
to
> I have a simple plot with 40 trends

I guess a legend to 40 plots would be very hard to read anyway. If
the figure is not to be printed (e.g. stay on a computer monitor), my
solution was to write a function so that each time you click on a
plot, the selected plot is highlighted and a label shows up.

You can download it here if that is of any interest to you:

<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=4031&objectType=FILE>

Jeff Daniels

unread,
Aug 10, 2005, 1:28:02 PM8/10/05
to
Well, I came up with a non-elegant solution.

1) I made a plot with a vertical legend with all 40 variables.
2) I captured the plot with Photoshop and rearranged the legend
elements into the configuration that I mentioned in the original
post. Then I exported the cropped legend to a JPG.
3) Finally, I added an additional axis to my figure and inserted the
legend with the IMAGE function and then turned the axes 'Visibility'
to 'off'.

Not the ideal solution, but it works! Just hope my variable names in
the legend don't change!

JD

Jérôme

unread,
Aug 11, 2005, 3:25:39 AM8/11/05
to
Hi,

I played a while with your problem :

figure

hold on

p=zeros(1,40);
str=cell(1,40);

for n=1:40

p(n)=plot(rand(1,10),rand(1,10));
set(p(n),'color',rand(1,3));
str{n}=sprintf('%d',n);

end

l=zeros(1,40);

for n=1:40

ax=axes('visible','off')
l(n)=legend(ax,p(n),str{n});

end

pos=get(l(1),'position');

for n=2:20

set(l(n),'position',[pos(1) pos(2)-(n-1)*pos(4) pos(3) pos(4)])

end

pos(1)=pos(1)+pos(3);

for n=1:20

set(l(n+20),'position',[pos(1) pos(2)-(n-1)*pos(4) pos(3) pos(4)])

end

That's NOT very good but I think that's the idea.
I don't optimize this code so it's more readable.
I think you can play with the position of each legend to stack them
in different order.

Jérôme

0 new messages