How to add legends from two plots as one?

I have a simple algorithm:
%%%%%%%%%%%%%%%%%%%%%%
%% making stickfigure of first position
a1=[ankxA,kneexA,hipxA,shouxA];
Unrecognized function or variable 'ankxA'.
b1=[ankyA,kneeyA,hipyA,shouyA];
a2=[ankx(1,2),kneex(1,2),hipx(1,2),shoux(1,2)];
b2=[anky(1,2),kneey(1,2),hipy(1,2),shouy(1,2)];
......
%% define legends
kne1=['Kne = ',num2str(kneStart(1))];
kne2=['Kne = ',num2str(kneStart(2))];
hoft1=['Hoft = ',num2str(hipStart(1))];
hoft2=['Hoft = ',num2str(hipStart(2))];
......
%% plots
subplot(3, 3, 1);
plot(a1, b1, 'b-');
hold on;
plot(a2, b2, 'r-');
axis ij;
legend(kne1,kne2,hoft1,hoft2,'location','best');
%%%%%%%%%%%%%%%%%%%%%%
But this doesnt work, because it only write the first legend. An easy way to fix this would be apreciated...

4 Comments

Hi,
Can you attach screenshots of the figure, so that the problem can be better understood?
dpb
dpb on 21 Jun 2023
Edited: dpb on 21 Jun 2023
It's certainly not clear what you would even expect at that point with the legend arguments you've given -- only two lines have been drawn on one axes of the subplot of nine (3x3) total axes, so the most you can add as a legend to that current axes will be two labels.
Just because you built the a, b vectors from four components, there's no relation of the number of points in a line to the line/array itself--the line is one thing and has one label; the points are something else again. If the idea is to plot the four points and label them, then you'll have to use a technique that plots each point as a separate object so they each have a handle and thus each will have a 'DisplayName' property and legend will be able to associate a label to each handle in the axes.
Thx all for answer so fast. I know Im newbie here, so again thanks. Attached should be an example of what shows on the subplot for now ..
Unfortunately, that s vanishingly small, and so difficult to read.
What result do you want?
figure
imshow(imread('legendErr.jpg'))

Sign in to comment.

Answers (0)

Products

Release

R2022b

Asked:

on 21 Jun 2023

Commented:

on 22 Jun 2023

Community Treasure Hunt

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

Start Hunting!