matlablegendmatlab-figure

How can I make two legends in a MATLAB figure the same size and adjust the legend marker size appropriately?


From the figure below, the following two issues are observed:

  1. Legend font size inconsistency: Although both Legend 1 and Legend 2 are set to font size 6, they appear to display at different sizes.
  2. Oversized legend markers: Adjusting legend2.ItemTokenSize has little effect on the size of the markers in the legend, which still appear too large.

How to solve the above problems? I would greatly appreciate it if you could provide assistance. Thanks a lot!

fig1

This is the code for legend1:

    legend1=legend(legend_entries, ...
                          'FontName', 'Times New Roman', 'FontSize', 6,...
                          'Location', 'NorthEastoutside',...
                          'Box', 'on', 'NumColumns', 1 );
    
    set(legend1, 'Position', [0.84, 0.41, 0.1, 0.2]); 

    
    legend1.ItemTokenSize = [10, 10];

    
    legend('AutoUpdate', 'off');
   

This is the code for legend2:

if nargin > 1 && ~isempty(set_legend2)
        
        new_ax = axes('Position', get(gca, 'Position'),...
                      'Units', get(gca, 'Units'), 'Visible', 'off');
        legend2=legend(new_ax,set_legend2,plot_info.leg,...
                              'FontName', 'Times New Roman', 'FontSize', 6, ...    
                              'Location', 'northwest', ...
                              'Box', 'off', 'NumColumns', 2 );
   
        set(legend2, 'Position', [0.55, 0.75, 0.1, 0.1]); 
         
        % 调整图例大小和位置
        legend2.ItemTokenSize = [10, 10]; 
    end

Solution

  • I don't think the font sizes are actually different, I think it's just because legend1 has entries in all capital letters which appear larger. They are both using font size 6. If I snip bits of the image together it looks like the actual character height is the same between the two, I think your (1) is a non-issue.

    font size check

    For the marker sizing, you can control it independently by setting the MarkerSize property of the line objects within the legend:

    % Create a plot
    figure(1); clf; hold on;
    plot( 1:N, cos((1:N)*2/N), '-x' );
    plot( 1:N, rand(1,N), '-o' );
    plot( 1:N, sin((1:N)*2/N), '-d' );
    
    % Create the legend, use the 2nd output to get the graphics objects
    [leg,obj] = legend( {'cos','rand','sin'} );
    % Find the line objects and change their marker properties
    lines = findobj(obj,'type','line');
    set(lines,'Markersize',15);
    

    Noted by @Ramashalanka in the comments, the multi-output syntax for legend has been recommended against for a while now (although MathWorks seem to have dropped the warning testing in 22b), here's an alternate method for getting the lines handles to then change the markers in the same way:

    % Create the legend
    leg = legend( {'cos','rand','sin'} );
    % Find the line objects and change their marker properties
    lines = cellfun( @(x)findobj(gcf,'displayname',x), leg.String );
    set(lines,'Markersize',15);
    

    big markers