MATLAB Answers

Displaying matrix of correlation coefficients

152 views (last 30 days)
Sumera Yamin
Sumera Yamin on 16 Dec 2019
Edited: Adam Danz on 23 Dec 2020
Hi, i have a large matrix of correlation coefficients, which i am displaying using imagesc function. It gives me the imageof the matrix with colours representing each element of thematrix. Is there some way to represent each entry of the matrix as individual circles instead of pixels.Please see the attached imagewhich is what i want to get. The code below representswhat i presently have
C=rand(20,20);
imagesc(C)
colormap(gca,'parula');
colorbar() ; % Add color bar and make sure the color ranges from 0:1
caxis([-1,1]);
download.png

Accepted Answer

Adam Danz
Adam Danz on 16 Dec 2019
Edited: Adam Danz on 23 Dec 2020
I'm not aware of a Matlab function that produces such a plot, It appears that the circles are scaled by color and size.
This code produces a similar plot. See inline comments for details.
% Produce the input matrix data
C=rand(20,30);
% Set [min,max] value of C to scale colors
clrLim = [0,1]; % or [-1,1] as in your question
% Set the [min,max] of diameter where 1 consumes entire grid square
diamLim = [0.3, 1];
% Plot the data using imagesc() for later comparison
figure()
imagesc(C)
colormap(gca,'parula');
colorbar();
caxis(clrLim);
axis equal
axis tight
% Compute center of each circle
% This assumes the x and y values were not entered in imagesc()
x = 1 : 1 : size(C,2); % x edges
y = 1 : 1 : size(C,1); % y edges
[xAll, yAll] = meshgrid(x,y);
% Set color of each rectangle
% Set color scale
cmap = parula(256);
Cscaled = (C - clrLim(1))/range(clrLim); % always [0:1]
colIdx = discretize(Cscaled,linspace(0,1,size(cmap,1)));
% Set size of each circle
% Scale the size in the same way we scale the color
diamSize = Cscaled * range(diamLim) + diamLim(1);
% diamSize = abs(C) * range(diamLim) + diamLim(1); for [-1,1] scaling
% Create figure
fh = figure();
ax = axes(fh);
hold(ax,'on')
colormap(ax,'parula');
% Create circles
theta = linspace(0,2*pi,50); % the smaller, the less memory req'd.
h = arrayfun(@(i)fill(diamSize(i)/2 * cos(theta) + xAll(i), ...
diamSize(i)/2 * sin(theta) + yAll(i), cmap(colIdx(i),:)),1:numel(xAll));
axis(ax,'equal')
axis(ax,'tight')
set(ax,'YDir','Reverse')
colorbar()
caxis(clrLim);
Compare figures
  7 Comments
emami.m
emami.m on 23 Dec 2020
Great job Adam Danz.
Just there is a small issue in your code. When correlation coefficients are [-1,1] as allowable range, the circle sizes are not correct. Circle sizes should be defined based on absolute values of correlations. So circle diameters should be:
% Set size of each circle
% Scale the size between [0 1]
diamSize = abs(C) * range(diamLim) + diamLim(1);
Also triangular display provided [Here] , thanks to your code:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!