Legend error. What did I do wrong here?

조회 수: 37 (최근 30일)
Hai Nguyen
Hai Nguyen 2020년 10월 2일
댓글: Adam Danz 2020년 11월 2일
I have 9 figures as per attached to make comparisons for the length, width and height using similar codes as below (this one was used for the width comparison).
I don't know why the legend for the plots of length and height comparions are correct but it is wrong for the width comparison. How to fix please?
if true
fig = figure(16);
ax = axes(fig);
xlabel('\xi');
ylabel(' \omega(\xi,\tau)');
title('Varying injection rates vs fracture width');
h1 = openfig('width0.fig','reuse');
h2 = openfig('width1.fig','reuse');
h3 = openfig('width2.fig','reuse');
copyobj(h1.Children.Children,ax);
copyobj(h2.Children.Children,ax);
copyobj(h3.Children.Children,ax);
close(h1);
close(h2);
close(h3);
end
hold on
legend('\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast')
  댓글 수: 1
VBBV
VBBV 2020년 10월 2일
편집: VBBV 2020년 10월 2일
% if true
% code
%end
legend([h1(1),h2(2),h3(3)], '\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast')

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

채택된 답변

Adam Danz
Adam Danz 2020년 10월 2일
편집: Adam Danz 2020년 10월 2일
The "width" figures contain multiple lines on top of each other just like in your last question.
To demonstrate, open the width0.fig and look at the number of objects on the axes.
h = openfig('width0.fig');
h.Children.Children
% 2×1 Line array:
%
% Line
% Line
You'll see two line objects. If you only want to copy one of them,
copyobj(h.Children.Children(1),ax);
% ^^^
  댓글 수: 9
Hai Nguyen
Hai Nguyen 2020년 11월 2일
Sorry I still can't do as you instructed. It seems that I copied all the lines of each object for the legend again and the explanations in it are still unchanged (data 1, data 2...).
Code is as below:
fig1 = figure();
ax1 = axes(fig1);
xlabel('\xi');
ylabel(' \omega(\xi,\tau)');
title(ax1,'Varying injection rates vs fracture width');
leg=legend(ax1,'\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast');
h1 = openfig('width0.fig','reuse');
h2 = openfig('width1.fig','reuse');
h3 = openfig('width2.fig','reuse');
copyobj(h1.Children.Children,ax1);
copyobj(h2.Children.Children,ax1);
copyobj(h3.Children.Children,ax1);
close(h1);
close(h2);
close(h3);
fig2=figure();
h = copyobj([ax1, leg], fig2);
title(h(1),'Varying injection rates vs fracture width');
Result:
Adam Danz
Adam Danz 2020년 11월 2일
"data 1, data2, data 3,..." appears in the legend when the graphics objects do not contain a value for DisplayName as my comment above indicates. If you didn't assign a DisplayName value when you created those lines, then you'll have to recreate the legend from scratch after copying the lines.
fig1 = figure();
ax1 = axes(fig1);
hold on
h1 = openfig('width0.fig','reuse');
h2 = openfig('width1.fig','reuse');
h3 = openfig('width2.fig','reuse');
copyobj(h1.Children.Children,ax1);
copyobj(h2.Children.Children,ax1);
copyobj(h3.Children.Children,ax1);
close(h1);
close(h2);
close(h3);
xlabel('\xi');
ylabel(' \omega(\xi,\tau)');
title(ax1,'Varying injection rates vs fracture width');
leg=legend(ax1,'\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast');
This assumes there 3 objects that are copied, since there are 3 strings defined in legend().

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

추가 답변 (0개)

카테고리

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