X-axis label on two rows

前端 未结 3 1494
轮回少年
轮回少年 2020-12-21 12:36
str = {\'HS31\'; \'HS31 (Ridotto)\';
    \'Dax85\';\'Dax85 (Ridotto)\';
    \'FTSE89\';\'FTSE89 (Ridotto)\';
    \'SP98\';\'SP98 (Ridotto)\';
    \'Nikkei22\';\'Nikk         


        
3条回答
  •  北海茫月
    2020-12-21 13:14

    Whats missing in the answer by Divakar is actually a solution with two (or more) rows. You can simulate it with text boxes at certain positions.

    str = {'HS31'; 'HS31 (Ridotto)';
        'Dax85';'Dax85 (Ridotto)';
        'FTSE89';'FTSE89 (Ridotto)';
        'SP98';'SP98 (Ridotto)';
        'Nikkei22';'Nikkei225 (Ridotto)';
        'SP457';'SP457 (Ridotto)';
        'Russ1318';'Russ1318 (Ridotto)';
        'Russ2151';'Russ2151 (Ridotto)';
        'Eurostoxx';'Eurostoxx (Ridotto)';
        'Ftse';'HS31 (Ridotto)';
        'Mibtel';'Mibtel (Ridotto)';
        'SP';'SP (Ridotto)';
        'Nasdaq';'Nasdaq (Ridotto)';};
    bar(rand(26, 1));
    % clear labels existing so far
    set(gca, 'XTickLabel', {});
    
    % parameters
    spacing = 0.03; % between different rows of labels and between x-axis and label
    rows = 2; % number of rows in staggered layout
    heightAdjust = 0.1; % reduce height of figure to make space for vertically stacked labels
    
    pos = get(gca,'Position');
    set(gca, 'Position', pos+ [0 heightAdjust 0 -heightAdjust] )
    
    ylim = get(gca, 'YLim');
    for i = 1 : length(str) % for all labels
        % compute y position of label (depends on x, therefore staggered)
        y = ylim(1) - spacing - spacing * mod(i - 1, rows);
        % print text box at correct position to simulate label
        text('Units', 'Data', 'Position', [i, y], 'HorizontalAlignment', 'center', 'String', str(i));
    end
    

    The number of rows and some spacing parameters have to be set manually. And it looks like this when the figure is maximized.

    Matlab figure with labels in rows

提交回复
热议问题