sparkle
sparkle

Reputation: 7390

X-axis label on two rows

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

I can't read the text below the plot. Do you know how to fix it?

Screenshot: https://www.dropbox.com/s/g43iegh4oeng3kf/computing_time.png enter image description here

Upvotes: 2

Views: 5088

Answers (3)

marsarius
marsarius

Reputation: 11

You could just type the control character \newline inside the label.

Upvotes: 0

NoDataDumpNoContribution
NoDataDumpNoContribution

Reputation: 10859

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

Upvotes: 1

Divakar
Divakar

Reputation: 221564

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

Upvotes: 9

Related Questions