Adding multiple legends outside for loop which is for multi variables
    7 次查看(过去 30 天)
  
       显示 更早的评论
    
Hello, I used two different colourbar for two different variables.
The variables are plotted in for loop. Legends should be outside for loop.
I cannot add multiple legend outside for loop.
I know that it must not recognise leg_copy.
However, I need to put all legends outside loop. Here is the code :
hold on
    clr = jet(alphasteps);        %# 1st colormap for first variable
    clr2 = copper(alphasteps);    %# 2nd colormap for second variable
%------------------------------------------
    for l = 1:alpha steps
    ...   ... ...
    end   
%------------------------------------------
    if exist ('t_clashing', 'var') & exist ('alpha_clashing', 'var')
        t_clashing
        alpha_clashing
        t_vect = 0:dt:max(t_clashing);
        figure(5);  
            for i=1:alphasteps
                hf1 = figure('Color', 'w');
                ax1 = axes('Parent',hf1);
                hold on;
                % First set of plots
                hplot1=plot(t_vect(1:t_clashing_index(i)-20), H1L(1:t_clashing_index(i)-20, i),'Color',clr(i,:))
                hf2 = figure('visible', 'on');
                ax2 = axes('Parent',hf2,'visible','on');
                hold(ax2, 'on');
                set(hplot1,'Parent',ax2);
                set(hleg_copy,'Parent',hf2);
                hplot2=plot(t_vect(1:t_clashing_index(i)-20),H1R(1:t_clashing_index(i)-20, i),'Color',clr2(i,:))
                hold on;
                set(gcf, 'Color',[1 1 1]);
                set(hplot1, 'lineWidth', 0.8);
                set(hplot2, 'lineWidth', 0.8);
            end
            xlabel('t')
            box on
        end
    end
%------------------------------------------
    hold off
    [hleg,hobjs,hlinpatches,legtxt] ...
        = legend(hplot1,'h_1L , \alpha = -2', 'h_1L , \alpha = -1.9','h_1L , \alpha = -1.8','h_1L , \alpha = -1.7','h_1L , \alpha = -1.6','location','NorthWest');
    hleg_copy = copyobj(hleg,hf1);
    delete(hleg);
    hleg_copy = copyobj(hleg,hf1);
    delete(hleg);
    [hleg2,hobjs2,hlinpatches2,legtxt2] = ...
       legend(hplot2,'h_1R , \alpha = -2', 'h_1R , \alpha = -1.9','h_1R , \alpha = -1.8','h_1R , \alpha = -1.7','h_1R , \alpha = -1.6','location','NorthEast');
    % Create a copy of the second legend and delete the original
    % This forces similar appearance and behavior for the two legends.
    hleg_copy2 = copyobj(hleg2,hf1);
    delete(hleg2);
    set(hleg_copy,'Tag','', 'ButtonDownFcn','', 'UserData', []);
    set(hleg_copy2,'Tag','', 'ButtonDownFcn','', 'UserData', []);
    set(hplot1,'Parent',ax1);
    set(hplot2, 'Parent', ax1);
    set(hleg_copy,'Parent',hf1);
    xlabel('X-Data');
    close(hf2);
0 个评论
采纳的回答
  Joachim Schlosser
    
 2016-3-29
        This seems to be previously answered in https://de.mathworks.com/matlabcentral/answers/97813-how-do-i-create-multiple-legends-to-correspond-to-multiple-sets-of-curves-in-my-matlab-figure
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
			
	产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

