How to see text outside image matlab

Viewed 48

I want to represent the values of a matrix as an image, adding some text in its margins. To do so, I proceed as follows:

matrix = rand(10);   %Suppose this matrix

figure('color','w')
imshow(matrix,[min(matrix(:)), max(matrix(:))])
colormap(gca,jet(256))
colorbar
truesize([400,400])
for i=1:size(matrix,1)
    text(-2,i,['long text ' num2str(i)], 'Interpreter', 'none')
    h = text(i,0,['column ' num2str(i)], 'Interpreter', 'none');
    set(h,'Rotation',90);
end

The original figure is:

enter image description here

I can see the text on the left increasing the figure width (with the mouse for instance):

enter image description here

However, if I increase the figure height as well, the image increases, and I cannot see the full text on the left and on the top at the same time.

I have 2 questions:

  1. Is there a neater way to represent a matrix including information outside like in this example?
  2. How could I see the full text properly?
1 Answers

For the first part of your question, I would do something like this, so that you don't have to keep track of the position of the labels:

matrix = rand(10);

figure('color','w')
imagesc(matrix) % plot your matrix
axis equal % correct aspect-ratio
xlim([0.5, 10.5])
colormap(gca, jet(256))
colorbar

% Create your labels
x = cell(1, size(matrix, 1));
y = x;
for i = 1:size(matrix, 1)
    x{i} = sprintf('Column %u', i);
    y{i} = sprintf('Long text %u', i);
end

% Edit your x axis
set(gca, ...
    'XTick', 1:size(matrix, 1), ... % select the ticks to be displayed
    'XTickLabel', x, ...            % select their labels
    'XTickLabelRotation', 90, ...   % rotate the labels vertically
    'XAxisLocation', 'top');        % put the x axis on top

% Similar for the y axis
set(gca, ...
    'YTick', 1:size(matrix, 1), ... % select the ticks to be displayed
    'YTickLabel', y);               % select their labels

For your second question, there is probably a way to automatically detect the optimal size of your axes, but you can simply do it by trial and error:

set(gca, 'OuterPosition', [0, 0.05, 1, 0.9]) % for example?

In particular, you might want to change the first and third element of the vector to modify the horizontal position and width (in fraction of the figure size), and the second and fourth element for the vertical position and height.

Related