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

4 views (last 30 days)
Alon Rozen on 19 Dec 2018
Commented: Alon Rozen on 19 Dec 2018
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?
Alon

madhan ravi on 19 Dec 2018
same the loop will run to 5
legend([h{1} h{3} h{5}], .... three texts)
Alon Rozen on 19 Dec 2018
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?
madhan ravi on 19 Dec 2018

madhan ravi on 19 Dec 2018
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})

Alon Rozen on 19 Dec 2018
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)
madhan ravi on 19 Dec 2018
can you give an example of your legend contents so that the solution can be narrowed down ?
Alon Rozen on 19 Dec 2018
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

R2016b

### Community Treasure Hunt

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

Start Hunting!