MATLAB Answers

Is it possible to create a selective legend in a multiplot graph?

1 view (last 30 days)
Alon Rozen
Alon Rozen on 31 Dec 2018
Commented: Alon Rozen on 1 Jan 2019
Hi all,
I already asked this question in somewhat less clear manner. So now I added a clear code:
I have two sets of data - A_Data and B_Data.
In each set the data has a data ID. So I have A_IDs and B_IDs.
I plot all on the same axis:
% Prepare:
H = My_Axis;
Num_Of_A = length(A_IDs);
Num_Of_B = length(B_IDs);
Lgnd_Cell = cell(Num_Of_A+Num_Of_B,1);
Lgnd_Indx = true(Num_Of_A+Num_Of_B,1)
% Plot A data:
for i = 1:Num_Of_A
Data = Data_A{i};
plot3(H,Data(:,1),Data(:,2),Data(:,3),['Certain_Shape' 'Certain_Color']);
hold on;
Lgnd_Cell{i} = ['A Data ' num2str(A_IDs(i))];
Lgnd_Indx(i) = Determine_If_To_Show_This_Data_Legend(Data);
end
% Plot B data:
for i = 1:Num_Of_B
Data = Data_B{i};
plot3(H,Data(:,1),Data(:,2),Data(:,3),['Certain_Shape' 'Certain_Color']);
hold on;
Lgnd_Cell{Num_Of_A+i} = ['A Data ' num2str(B_IDs(i))];
Lgnd_Indx(Num_Of_A+i) = Determine_If_To_Show_This_Data_Legend(Data);
end
Now, based on 'Lgnd_Indx' I want to create a selective legend.
Is it possible?
Thanks,
Alon

  4 Comments

Show 1 older comment
Alon Rozen
Alon Rozen on 31 Dec 2018
Hi,
It means that some of the legend items are to be displayed and some not - based on some logic.
Alon Rozen
Alon Rozen on 31 Dec 2018
The logic dosen't matter because it is not the source of the problem. The source is that the legend command marks the legend items orderly acording to their mark and color. If you ommit one the mark and color that you get belong to it and not to the next plot. So the legend will not corrlate the plots and their right mark + color.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 31 Dec 2018
record the handles returned by plot3. Index them by your Indx array as your first parameter to legend and index the cell by the same as your second parameter to legend

  1 Comment

Alon Rozen
Alon Rozen on 1 Jan 2019
I tried the following:
I created a cell array 'Plt_Hndl' which record the handles returned by plot3. This is the code:
Plt_Hndl{end+1} = plot3(......);
I also collected the legends I need into 'Lgnd_Cell';
To make things simple I had only two plot handles and two strings as legends.
Next I tried:
legend(Plt_Hndl,Lgnd_Cell);
It didn't work.
It can be done?

Sign in to comment.

Tags

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!