How can I generate a legend of two combined figures? I

조회 수: 7 (최근 30일)
Belinda
Belinda 2023년 9월 2일
댓글: Belinda 2023년 9월 6일
Hello everybody,
I wrote a Skript to combinen two separates figures of gyroscpic data of legs of horses. VR is right front leg HL is left hind leg.
I changed the colour of one into orange for being able to differentiate between them.
Now I am trying to add the legend that both lines are mentioned, but nothing I tried works.
I hope anybody can give me a resolution that works, thank you!
Files of figures are attached, skript is as followed:
close all, clear all;
%um die diagonalen Beinpaare in eine Abbildung zu bekommen, inkl
%Auftrittspunkte
VR= openfig('bueVR150GZ.fig', 'reuse');
HL= openfig('bueHL150GZ.fig', 'reuse');
b = get(gca,'child');
c = get(b,'Type');
b = findobj(gca,'Type','Line');
set(b,'Color',"#D95319");%changes colour
figure %two figures combined in one to visualise synchronicity of leg movement
h1=subplot(1,1,1);
h2=subplot(1,1,1);
copyobj(allchild(get(VR,'CurrentAxes')),h1 );
hold on
copyobj(allchild(get(HL,'CurrentAxes' )),h2);
xlim([0 inf]);
xlim tight;
%xticks(0:10:end)
grid on;
grid minor;
xlabel('Zeit [s]')
ylabel('Winkeländerung [dps]')
title('Auftrittspunkte des diagonalen Beinpaares','FontSize',12);
%legend({'Vorderbein (rechts)'}{'Hinterbein (links)'});
%hleg1 = legend(VR,"Messdaten");
%hleg2 = legend(HL);
%legend(hleg1, hleg2);
ax= gca;
ax.XTick = 0:1:150 ;%damit wir die beschriftung der xAchse eingestellt
%egend(sprintf(%s, ))%steht hier für character und sprintf steht sozusagen
%für einsetzen. dieses wird immer mit einem Befehlt/anweisungangegeben und
%dann, was in dieese anweissung für variablen/elemente eingesetzt werden
%sollten
%legend(get(gca, 'children'), get(get(gca, 'children')));
%set('XTick',min:1:max);
%XLim = get(gca, 'XLim');
%stepBy = 2.5;
%set(gca, 'XTick', XLim(1):stepBy:XLim(2))
%set(gca, "GridLineStyle", "--")
%savefig('1BueVRHLGyroz150AP.fig')

채택된 답변

dpb
dpb 2023년 9월 2일
편집: dpb 2023년 9월 2일
%um die diagonalen Beinpaare in eine Abbildung zu bekommen, inkl
%Auftrittspunkte
VR= openfig('bueVR150GZ.fig','invisible');
HL= openfig('bueHL150GZ.fig','invisible');
b = get(gca,'child');
c = get(b,'Type');
b = findobj(gca,'Type','Line');
set(b,'Color',"#D95319");%changes colour
figure %two figures combined in one to visualise synchronicity of leg movement
h1=axes;
copyobj(allchild(get(VR,'CurrentAxes')),h1);
hold on
copyobj(allchild(get(HL,'CurrentAxes')),h1);
xlim([0 inf]);
xlim tight;
%xticks(0:10:end)
grid on;
grid minor;
xlabel('Zeit [s]')
ylabel('Winkeländerung [dps]')
title('Auftrittspunkte des diagonalen Beinpaares','FontSize',12);
legend('Vorderbein (rechts)','Hinterbein (links)','location','southwest');
You were almost there. :) The problem is you created two axes on top of each other by using subplot and put the two in those separate axes instead of creating a (one) new axes into which to put both. And, legend only puts elements that are on the same axes in its list/display so you couldn't put but the one on each. With both in the one axes, then it works as desired.
  댓글 수: 3
dpb
dpb 2023년 9월 4일
편집: dpb 2023년 9월 4일
Well, the points were plotted as individual points so there are actually a zillion lines on each plot; identifying which are the two solid lines to tell legend which two out of all them to label is the problem...
VR= openfig('bueVR150GZ.fig','invisible');
HL= openfig('bueHL150GZ.fig','invisible');
b = get(gca,'child');
c = get(b,'Type');
b = findobj(gca,'Type','Line');
set(b,'Color',"#D95319");%changes colour
figure %two figures combined in one to visualise synchronicity of leg movement
h1=axes;
copyobj(allchild(get(VR,'CurrentAxes')),h1);
hold on
copyobj(allchild(get(HL,'CurrentAxes')),h1);
xlim([0 inf]);
xlim tight;
%xticks(0:10:end)
grid on;
grid minor;
xlabel('Zeit [s]')
ylabel('Winkeländerung [dps]')
title('Auftrittspunkte des diagonalen Beinpaares','FontSize',12);
hL=findobj(h1,'LineStyle','-')
hL =
2×1 Line array: Line (Messdaten) Line (Messdaten)
legend(hL,'Vorderbein (rechts)','Hinterbein (links)','location','southwest');
If the order is reversed (will depend on the order were initially plotted as to who's first in line), then just reverse the order of the text or the order of the handles.
The above handles show the two lines were labelled so if had known that a priori, could have searched for the specific text, but didn't know anything about that so just looked for the solid lines and, luckily, there are only two of those.
Belinda
Belinda 2023년 9월 6일
thank you so much, it works great now!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by