10

I'm trying to emulate this graph: enter image description here

If I have a correlation matrix how can I create an output like this?

zpesk
  • 4,343
  • 7
  • 39
  • 61

3 Answers3

8

If you have an n x n correlation matrix M, and a vector L of length n containing the label for each bin, you can use something like the following:

imagesc(M); % plot the matrix
set(gca, 'XTick', 1:n); % center x-axis ticks on bins
set(gca, 'YTick', 1:n); % center y-axis ticks on bins
set(gca, 'XTickLabel', L); % set x-axis labels
set(gca, 'YTickLabel', L); % set y-axis labels
title('Your Title Here', 'FontSize', 14); % set title
colormap('jet'); % set the colorscheme
colorbar on; % enable colorbar

Rotating x-axis labels is not trivial, but the MATLAB Central File Exchange contains some solutions.

blafrat
  • 339
  • 2
  • 7
  • For x-axis label rotation, you can easily do it via matlab figure window: 1. select *show plot tools and dock figures* button from toolbar, https://i.stack.imgur.com/lmiz1.png 2. click on x-axis labels on figure https://i.stack.imgur.com/63oKg.png 3. choose *more propertises...* from the appeared window https://i.stack.imgur.com/o8NRm.png 4. Navigate to *XTickLabelRotation* and set it 90.0 https://i.stack.imgur.com/FHjz7.png – forough Dec 14 '17 at 12:15
3

Adding to @Thomas C. G.'s answer, I'd use:

imagesc(myMatrix);
colormap(jet);
colorbar;

% then to set the axis titles you'll have to use
% Please note the curly braces for the cell array
labelNames = {'USA','NASDAQ','Dow Jones'};
set(gca,'XTickLabel',labelNames);   % gca gets the current axis
set(gca,'YTickLabel'labelNames);   % gca gets the current axis

Unfortunately, AFAIK, making the text labels vertical as they are in your figure is a bit harder. Maybe somebody else has knowledge to the contrary.

Chris A.
  • 6,817
  • 2
  • 25
  • 43
1

To plot a matrix as an image you just need to call two functions:

image(myMatrix)
colormap(jet)

The colormap function defines the colour pattern used to render the image. The image you posted is using the "jet" colormap.

And to show the colour scale beside the image use the colorbar function.

Thomas C. G. de Vilhena
  • 13,819
  • 3
  • 50
  • 44