legend code doesn't work

조회 수: 12 (최근 30일)
Gary B
Gary B 2016년 6월 10일
댓글: Star Strider 2016년 6월 10일
x = -pi:pi/20:pi; y1 = sin(x); y2 = cos(x); plot(x,y1,x,y2) l = legend('sin(x)','cos(x)'); title(l,'My Legend Title')
This code doesn't display the legend title. I've tried various "h." things, title(legend,"..."), title(string,"...") etc and the best I can get is to display some text above the legend (through the top axis). Using Matlab 2013 at work.

채택된 답변

Star Strider
Star Strider 2016년 6월 10일
You’re looking at the online documentation, that is for R2016a. In the documentation for the R2013a version of legend, the legend title property does not exist. If I remember correctly, it only became available in R2015b or R2016a.

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 10일
편집: Azzi Abdelmalek 2016년 6월 10일
x = -pi:pi/20:pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)
l = legend({'sin(x)','cos(x)'});
title(l,'My Legend Title')

Gary B
Gary B 2016년 6월 10일
Thanks, but that doesn't work. I even cut and pasted it.

Gary B
Gary B 2016년 6월 10일
Cheers!
Yes it's actually 2013b, but I'm sure it's the same.
That's a few hours of my life I won't get back! LOL
I guess I'll have to add text or some such.
  댓글 수: 1
Star Strider
Star Strider 2016년 6월 10일
A text object would work. Use the 'Position' property of the legend to calculate where to put the text object.
This will get you started:
x = -pi:pi/20:pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)
l = legend('sin(x)','cos(x)');
pos = get(l, 'Position');
ht = text(1,1, 'My Legend Title');
set(ht, 'Position',[pos(1)+pos(4)+1, pos(2)+pos(3)], 'VerticalAlignment','bottom', 'HorizontalAlignment','left')
You will obviously have to experiment to get the result you want with your plot. See the 'Position' property for details on the meaning of the values of the vector it returns. Note that you have to use initial (x,y) coordinates to plot the text, and they can be anything. Then use the 'Position' property to put it wherever you want it.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by