两行的X轴标签

时间:2014-05-19 20:49:23

标签: matlab plot label matlab-figure

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)';

};
h=figure;
bar(t);
set(gca, 'XTickLabel',str, 'XTick',1:numel(str))

saveas(h, 'Backtesting/computing_time.png');

我无法阅读情节下方的文字。你知道怎么解决吗?

屏幕截图:https://www.dropbox.com/s/g43iegh4oeng3kf/computing_time.png enter image description here

3 个答案:

答案 0 :(得分:9)

如果你可以使用90度旋转文字,你可以尝试使用基于非常有用的x标签旋转文本工具的代码here

<强>代码

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');

包含一些随机数据的示例图

enter image description here

如果您可以对x标签文本进行下采样,例如仅显示所有其他文本 label,在创建图形句柄之前使用此权限 -

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

其余代码保持不变。输出图看起来像这样 -

enter image description here

如果您仍希望保持数据水平,则需要通过良好因素对标签数量进行下采样。在给定的示例中,4因子有效。代码中的更改是在声明str后立即添加以下代码,当然还要评论x标签旋转工具的使用情况 -

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

这里的技巧是为您要跳过的x标签使用空单元格。

结果 -

enter image description here

答案 1 :(得分:1)

Divakar在答案中缺少的实际上是一个包含两行(或更多行)的解决方案。您可以在特定位置使用文本框进行模拟。

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

必须手动设置行数和一些间距参数。当数字最大化时,它看起来像这样。

Matlab figure with labels in rows

答案 2 :(得分:0)

您只需在标签内键入控制字符\newline