Displaying matrix of correlation coefficients

201 views (last 30 days)
Sumera Yamin
Sumera Yamin on 16 Dec 2019
Commented: ack on 9 Sep 2021
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 8 Sep 2021
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
% This must span the range of your data!
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.
% If you get an error in this line below, "Index in position 1 is invalid",
% that probably means you didn't set clrLim correctly.
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);
Update
In Matlab R2020b bubblechart() was released which produces similar plots. However, the size of the circles are in point units where one point equals 1/72 inch and currently there is no other unit options. This means that when the figure or axes are resized, the circles will not maintain the axis-relative size. It would be nice if bubblechart supported data-units for its size parameter in a future release.
Here's what the plot would look like in R2021a using the same variables produced above.
figure();
bubblechart(xAll(:), yAll(:), diamSize(:), colIdx(:), ...
'MarkerEdgeColor','k', 'MarkerEdgeAlpha',1, ...
'MarkerFaceAlpha',1)
bubblesize([3,12]) % <-- customized for the display size below
axis equal
axis tight
set(gca, 'ydir','reverse') % for direct comparison with imagesc plot
  21 Comments
ack
ack on 9 Sep 2021
Thank you for the detailed responses and suggestions. I do checked all the functions use in your code before using it but still learning how to make sense of the whole thing. Yes, I want to visualize the correlation between the variables in T1. The code for correlation coefficient color bar change works and I have also match the circle colors to blue. Many thanks!

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!

Translated by