How to control appearance of certain graphs legend in a multi graph plot?

Hi all,
I have the following code which produce a multi fraph plot on an axes which I identify as 'H'
Legend_Data = cell(Some_Number);
for i = 1:Some_Number
plot3(H,X,Y,Z,[Marker,Color]);
Legend_Data{i} = 'Some text';
end
'H' is the axes (in a GUI) in which I want to plot all the graphs.
In each iteration the vectors X,Y,Z are different as well as the sting 'Some text'. Also, the marker used for the graph and its color are changed.
At the end, based on some logic, I wand to desplay a legend but to exclude some of entries. Of course, I want the remaining data to be correctly associated with the right graphs.
I read some sugesstions over the net but couldn't figure out how to apply them to this code. Most of them were related to cases where all graphs are ploted at once.
Can it be done?
Thanks in advance,
Alon

7 Comments

If you create a handle for plot3() at th end after the loop you can remove unwanted legends entries also by keeping the order unchanged like shown https://in.mathworks.com/help/matlab/ref/legend.html#bu_sz6u-1
Hi madhan,
I don't fully understand. Can you add a code to descrive how to do that?
Alon
x=rand(10,2);
y=rand(10,2);
z=rand(10,2);
h=cell(1,2);
for i = 1:2
h{i}=plot3(x(:,i),y(:,i),z(:,i));
hold on
end
legend(h{2},'some text')
Thanks madhan,
It took me closer to solution.
I had problem to show legend of more then one graph.
I tried to iterate the command 'legend(h(i),'some text i') but each time it overwrite the same line.
Suppose in your example there are 5 lines. How to I get the legend of lines, lets say, 1,3,5?
Thanks,
Alon
same the loop will run to 5
legend([h{1} h{3} h{5}], .... three texts)
I don't knoe beforehand what plots I will need. It depends on some calculations. So I can't prepare exactly the ones I will want to use. Sometimes it will be this number and sometime a different number. I will find the i's which suit presentation. Can I automate the 'legend' command?

Sign in to comment.

Answers (1)

Comment reposted as answer:
x=rand(10,5);
y=rand(10,5);
z=rand(10,5);
h=cell(1,5);
c={'a','b','c','d','e'};
for i = 1:5
h{i}=plot3(x(:,i),y(:,i),z(:,i));
hold on
end
legend([h{1} h{3} h{5}],c{1},c{3},c{5})

3 Comments

What if you don't know beforehand how many, and which, of the legend you want to keep?
Suppose it it based on some calculations you do in each loop?
How can you automate the inside of the command 'legend'?
(I tried some ideas but fail to solve)
can you give an example of your legend contents so that the solution can be narrowed down ?
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_A
Data = Data_A{i};
plot3(H,Data(:,1),Data(:,2),Data(:,3),['Certain_Shape' 'Certain_Color']);
hold on;
Lgnd_Cell{Num_Of_A+i} = ['A Data ' num2str(A_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
Hope it explains.
Alon

Sign in to comment.

Products

Release

R2016b

Asked:

on 19 Dec 2018

Commented:

on 19 Dec 2018

Community Treasure Hunt

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

Start Hunting!