how to plot Correlation matrix between several variable with circle?

10 次查看(过去 30 天)
I need a Correlation matrix plot same as this
is it anyone can help me?

回答(1 个)

Cris LaPierre
Cris LaPierre 2022-2-20
There is no single function for creating this plot, so you would have to build it up using bits and pieces from several functions. Here is an example that should get you started. It uses
% create sample data set
X = randn(30,4);
Y = randn(30,4);
Y(:,4) = Y(:,4)+X(:,2);
[rho,pval] = corr(X,Y)
rho = 4×4
-0.0176 0.0339 -0.3074 -0.5025 -0.1188 0.0131 0.3029 0.6064 -0.3706 0.0243 0.0182 0.1292 -0.1012 -0.2620 -0.1494 -0.4336
pval = 4×4
0.9263 0.8590 0.0984 0.0047 0.5317 0.9452 0.1038 0.0004 0.0438 0.8985 0.9241 0.4963 0.5948 0.1620 0.4309 0.0167
% gray squares via imagesc
c = double(pval>=0.05);
c(c==0)=0.8; % result would be white/black, so change black to gray
imagesc(c,[0 1]) % imagesc autoscales to the data range, so force range of [0 1]
colormap gray
xticks(1:size(c,2))
yticks(1:size(c,1))
axis equal
axis tight
xline(1.5:size(c,2)-0.5,'--','Color',[0.8 0.8 0.8]) % xgrid
yline(1.5:size(c,1)-0.5,'--','Color',[0.8 0.8 0.8]) % ygrid
% add bubbles
hold on
[X,Y] = meshgrid(1:size(c,2),1:size(c,1));
bubblechart(X(rho(:)<0),Y(rho(:)<0),rho(rho(:)<0),'MarkerFaceAlpha',0.20) % blue
bubblechart(X(rho(:)>=0),Y(rho(:)>=0),rho(rho(:)>=0),'MarkerFaceAlpha',0.20) % red
% set min/max bubble size
bubblesize([5 20])
bubblelegend('Location','eastoutside')
hold off
  2 个评论
Mahboubeh Molavi-Arabshahi
thanks, it helps me so much.
how i can change colorbor
why it is not from -1 to 1
what's mean the grey boxes
how i can omit the diagnal
Cris LaPierre
Cris LaPierre 2022-2-23
编辑:Cris LaPierre 2022-2-23
There is no colorbar. That is a legend for a bubblechart. You can explore colorbar options, but the problem is your plot does not have a range of colors that would make having a colorbar necessary. It has 2 hard-coded colors (red and blue), making a legend more appropriate. It is not from -1-1 because it is a legend of the data in your plot, not a colorbar.
Instead, it was my guess that it was the size of the bubble that indicated the strength of the correlation.
You should review the image you shared to determine what the grey boxes mean. I based this figure off that.
I did overlook the diagonal. Not promising this is the best apprach, but here's one way.
% create sample data set
X = randn(30,4);
Y = randn(30,4);
Y(:,4) = Y(:,4)+X(:,2);
[rho,pval] = corr(X,Y)
rho = 4×4
0.2420 0.2360 0.0518 0.1984 0.1743 0.1636 0.0951 0.5734 -0.2812 0.3406 0.1288 0.6245 0.4104 -0.1279 0.0208 0.0882
pval = 4×4
0.1976 0.2092 0.7857 0.2932 0.3568 0.3876 0.6173 0.0009 0.1322 0.0655 0.4977 0.0002 0.0243 0.5007 0.9131 0.6431
% gray squares via imagesc
c = double(pval>=0.05);
d = diag(ones(length(pval),1));
c(d==1)=1;
c(c==0)=0.8; % result would be white/black, so change black to gray
imagesc(c,[0 1]) % imagesc autoscales to the data range, so force range of [0 1]
colormap gray
xticks(1:size(c,2))
yticks(1:size(c,1))
axis equal
axis tight
xline(1.5:size(c,2)-0.5,'--','Color',[0.8 0.8 0.8]) % xgrid
yline(1.5:size(c,1)-0.5,'--','Color',[0.8 0.8 0.8]) % ygrid
% add bubbles
hold on
[X,Y] = meshgrid(1:size(c,2),1:size(c,1));
bubblechart(X(rho(:)<0 & d(:)==0),Y(rho(:)<0 & d(:)==0),rho(rho(:)<0 & d(:)==0),'MarkerFaceAlpha',0.20) % blue
bubblechart(X(rho(:)>=0 & d(:)==0),Y(rho(:)>=0 & d(:)==0),rho(rho(:)>=0 & d(:)==0),'MarkerFaceAlpha',0.20) % red
% set min/max bubble size
bubblesize([5 20])
hold off

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by