X-axis label on two rows

前端 未结 3 1505
轮回少年
轮回少年 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:22

    If you are okay with 90 degrees rotated text, you may try this code that is based on a very useful x-label rotating text tool, available here

    Code

    h=figure;
    bar(randi(9,26,1),'EdgeColor','g') %// Assumed random data for demo
    set(gca, 'XTickLabel',str, 'XTick',1:numel(str))
    xticklabel_rotate([],90,str);%% File-exchange code %// xlabel text rotated
    saveas(h, 'computing_time.png');
    

    Sample plot with some random data

    enter image description here

    If you are okay with down-sampling the x-label text, i.e. for example show only every other label, use this right before creating the figure handle -

    str(1:2:end)={[]}
    

    Rest of the code stays the same. The output plot would look like this -

    enter image description here

    If you still want to keep the data horizontal, you need to downsample the number of labels by a good factor. In your given sample case, a factor of 4 worked. The changes in the code is adding the following code right after declaring str and of course commenting the x-label rotating tool usage -

    str1 = cell(1,numel(str));
    str1(1:4:end) = str(1:4:end);
    str = str1;
    

    The trick here is to use empty cells for the x-labels that you want to skip.

    Result -

    enter image description here

提交回复
热议问题