Problem in specifying legend in multiple lines and symbols plot
    6 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
I've created a MATLAB script to generate a plot with four lines of different colors and three symbols. Now, I need to add specific legends: the four color lines (green, blue, black, red) should represent the variables stored in `D_cond` as {'Mno','Mot','Mor','Cse'}, and below that the three symbols of black color (pentagon, square, circle) should represent those stored in `C_con` as {'Pr','7 s','10 s'}. Can anyone please help me to get this legends? I have also attached the output as legends what I want as image.
data           = xlsread("me.xlsx", "Sheet1",'A:D')
k = figure;
for loop2 = 1:4
    values = data(:,loop2);% 5.9413
    me = values'
    vel_end = 4;
    ve1 = 0:vel_end/40:vel_end
    xaxis = sort(ve1');
    SD = [0.1 0.1 0.1];%[0.0568 0.0719 0.1002];% 0.109];
    marker_symbols = {'o', 's', 'p'}; % Add more marker symbols if needed
    colors = {[0 0.5 0], [0 0 1], [0 0 0], [1 0 0]}; % Specify colors for each loop iteration
    for loop1 = 1:3
        for loop = 1:length(ve1) % Change the increment to 20
            sink(loop, 1) = (log(xaxis(loop, 1) / me(1, loop1))) / SD(1, loop1);
            yaxis1(loop, 1) = normcdf(sink(loop, 1));
        end                                                                                                                                                                                                 
        hold on;
        plot(ve1, yaxis1, 'Color',[colors{loop2}], 'Marker', marker_symbols{loop1}, 'MarkerFaceColor', [1 1 1], 'MarkerSize', 6, 'LineWidth', 1.2);
        hold on
        D_cond  = {'Mno','Mot','Mor','Cse'}
        C_cond = {'Pr','7 s','10 s'}
        hold off 
    end
end
0 commentaires
Réponses (1)
  Lokesh
      
 le 26 Avr 2024
        Hi Raj,
I understand that you are looking to add a specific legend to your MATLAB plot that combines both color and symbol information. For example, a green line with a pentagon symbol should be labeled 'Mno + Pr', combining elements from two variables, 'D_cond' and 'C_cond'.
Given that an 'Axes' object can only have one legend, a workaround is to arrange the labels in a sequence that mirrors the order of the plotted data, effectively integrating both color and symbol information into each label. This approach ensures that each plot is accurately represented in the legend. Here’s how you can implement this
data = xlsread("me.xlsx", "Sheet1",'A:D');
D_cond = {'Mno', 'Mot', 'Mor', 'Cse'};
C_cond = {'Pr', '7 s', '10 s'};
k = figure;
legendEntries = cell(1, numel(D_cond) * numel(C_cond));%cell array used to store the legend entries for each curve plotted in the loop. 
idx=1; % Index to populate legendEntries
for loop2 = 1:4
    values = data(:,loop2);
    me = values';
    vel_end = 4;
    ve1 = 0:vel_end/40:vel_end;
    xaxis = sort(ve1');
    SD = [0.1 0.1 0.1];
    marker_symbols = {'o', 's', 'p'};
    colors = {[0 0.5 0], [0 0 1], [0 0 0], [1 0 0]}; 
    for loop1 = 1:3
        for loop = 1:length(ve1) 
            sink(loop, 1) = (log(xaxis(loop, 1) / me(1, loop1))) / SD(1, loop1);
            yaxis1(loop, 1) = normcdf(sink(loop, 1));
        end                                                                                                                                                                                                 
        hold on;
        plot(ve1, yaxis1, 'Color',[colors{loop2}], 'Marker', marker_symbols{loop1}, 'MarkerFaceColor', [1 1 1], 'MarkerSize', 6, 'LineWidth', 1.2);
        legendEntries{idx} = [D_cond{loop2}, ' + ', C_cond{loop1}]; % Store legend entry for current curve
        idx=idx+1;% Increment index for next legend entry
        hold on
        hold off 
    end
end
legend(legendEntries, 'Location', 'best');% Display legend with entries for each curve
%Refer to the attachment for the final plot
Refer to the following documentation for more information on 'legend':
Voir également
Catégories
				En savoir plus sur Legend dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

