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

4 ビュー (過去 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 Center および File ExchangeLegend についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by