Displaying matrix of correlation coefficients
55 views (last 30 days)
Show older comments
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/254651/download.png)
0 Comments
Accepted Answer
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
Adam Danz
on 9 Sep 2021
> Does this implies that my dataset is not very suitable to run correlation
No. The solution in my answer produces a visualization of correlations (a corellogram) or of any rectangular dataset. It does not compute correlations.
> I just put in the T1 into corrcoef function to make it into square matrix like yours and now its working.
It sounds like you're just trying stuff until you get a figure that looks nice. I see this approach a lot and it's not how data should be analyzed and interpretted. For example, do you want to visualize T1 or do you want to visualize the correlations between the variables in T1? That decision should be made before you start write code. Secondly, you shouldn't use other people's code without reading it and making sense of it. Even if you don't understand every line, it's important to get a sense of what the code does. I know this takes some skill that newbies may not yet have but that's where Matlab documentation comes in handy. If you can't understand what the code is doing you'll never be able to make small adjustments to the code to customize it to your needs.
> Not sure about the accuracy of the result though
clrLim=[-1,1], that part is fine and will not affect any kind of accuracy. It's just used to set the color limits. Actually no part of my solution is related to accuracy. It just produces a visualization of data.
> And how can I change the color for correlation coefficient 1 to blue and -1 to red
Try this
nbins = 10;
rgb = [ones(ceil(nbins/2),1),linspace(0,1,ceil(nbins/2))' .* [1,1]];
rgb = [rgb; rot90(rgb(1:floor(nbins/2),:),2)];
colormap(ax,rgb);
% or
colormap(ax,flipud(rgb))
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!
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!