# Setting legend of a plot

조회 수: 21 (최근 30일)
Shahriar Shafin 2024년 1월 30일
댓글: Star Strider 2024년 1월 31일
How do I set up a legend like this(two curves one legend then some gap)??
##### 댓글 수: 2없음 표시없음 숨기기
VBBV 2024년 1월 31일
The plots you show have custom legends.
Try this link available in FEX where you can change the legend layout and behavior as you wanted
Dyuman Joshi 2024년 1월 31일
Why not plot an empty line along side and use a blank as its legend entry?
x = 0:0.01:10;
figure
plot(x, sin(x))
hold on
plot(x,cos(x));
plot(x,floor(x/5))
plot(x,NaN(size(x)))
xlim([-1 11])
str = ["sin" "cos" "custom" ""];
legend(str, 'NumColumns', 2)

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

### 채택된 답변

Star Strider 2024년 1월 30일
Set the original legend to 'Location','NE' and then use:
legend('boxoff')
Also, see Position Multiple Axes in Figure if you have not done so already.
##### 댓글 수: 6이전 댓글 4개 표시이전 댓글 4개 숨기기
Shahriar Shafin 2024년 1월 31일
@Star Strider got it ,thanks a lot.
Star Strider 2024년 1월 31일
As always, my pleasure!

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

### 추가 답변 (2개)

Image Analyst 2024년 1월 30일
I think you'd have to build it up manually with the plot and text functions.
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

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

VINAYAK LUHA 2024년 1월 30일
Hello Shahriar,
It looks like you want to overlay two sets of axes, with the larger one encompassing the smaller, and include a common legend for both in MATLAB. To accomplish this task, adhere to the following instructions:
• First, Create an axes called "larger" and add plots to it.
• Next, define a another axes called "smaller" to be positioned within the "larger" axes.
• Finally,to create a common legend, plot invisible lines and assign the legends to them.
Here's the MATLAB code for your reference:
x = linspace(1, 10, 100);
y1 = log(x);
y2 = log(x.^2);
y3 = log(x.^3);
y4 = log(x.^4);
figure;
larger = axes;
plot(larger, x, y1, 'r-', x, y2, 'b-', x, y3, 'g-', x, y4, 'k-');
hold(larger, 'on');
smaller = axes('Position', [0.2 0.6 0.25 0.25]);
plot(smaller, x, y1, 'r-', x, y2, 'b-', x, y3, 'g-', x, y4, 'k-');
h1 = plot(larger, NaN, NaN, 'r-');
h2 = plot(larger, NaN, NaN, 'b-');
h3 = plot(larger, NaN, NaN, 'g-');
h4 = plot(larger, NaN, NaN, 'k-');
legend(larger, [h1, h2, h3, h4], {'log(x)', 'log(x^2)', 'log(x^3)', 'log(x^4)'});
Hope this helps you to understand how to have a common legend for two nested axes
Regards
Vinayak Luha

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

### 카테고리

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