Fancy Correlation Plots in MATLAB
이전 댓글 표시
I'm trying to find a way to generate these pretty correlation plots in MATLAB. These are generated in R using 'corrplot' function, but couldn't find any similar code in MATLAB. Any help would be appreciated.
As a quick description, this function will create a color scale of the correlation values, and create circles in each cell of the correlation matrix/plot with the associated color. The size of the circles is also an indicator of the magnitude of the correlation, with larger circles representing a stronger relationship (positive or negative). More details could be found here.

댓글 수: 1
Mathieu NOE
2020년 12월 22일
hi
you should be able to get this by combining
corrcoef
and
채택된 답변
추가 답변 (2개)
댓글 수: 8
Mathieu NOE
2020년 12월 23일
excellent work !!
Adam Danz
2020년 12월 23일
Some suggestions,
1. Avoid using length(), especially with matricies and multidimensional arrays.
tickvalues = 1:size(C,2); % <----
x = zeros(size(tickvalues));
text(. . .);
x = size(C,1)+1; % <----
text(. . .);
2. Use ticklabels instead of text and keep the axes on. Otherwise if you want to change your axis limits or zoom into something on the axes you'll lose the positioning of the text relative to the axis lines.
tickvalues = 1:size(C,2);
set(ax,'XTick',tickvalues,'XTickLabel',myLabel)
set(ax,'YTick',tickvalues,'YTickLabel',myLabel)
ax.XAxis.TickLabelRotation = 90;
Adding grid on may help with visual aligmnet of smaller markers.
3. Why reverse the y axis? Normally in correlation plots point (n,n) refers to the same conditions along the x and y axes.
4. Minor, but label the colorbar
cb = colorbar();
ylabel(cb, 'correlation coefficient')
With these suggestions,

Mathieu NOE
2020년 12월 23일
.... and remove / comment axis off
otherwise you lose all axis labeling, tick marks and background
emami.m
2020년 12월 23일
Mathieu NOE
2020년 12월 24일
you're welcome !
Martin Vallejos
2021년 5월 1일
I have a question about the last graph in matlab, can you show the values as in the second graph of R? (the graph of R is in the first comment)
Ziwei Liu
2023년 7월 25일
Thanks for sharing this and it helps me a lot. Just one tiny thing I found out during my time playing with this script is that the following lines:
Cscaled = (C - clrLim(1))/range(clrLim); % always [0:1]
colIdx = discretize(Cscaled,linspace(0,1,size(cmap,1)));
may cause a problem if (1) clrlim is switched to [0 1] and (2) the max of C is larger than 1. In this case the current Cscaled line actually does not scaled C at all, resulting in NaN values in colIdx at positions i where C(i) > 1. I modified the Cscaled line to overcome this problem:
Cscaled = (C - min(C(:)))/(max(C(:))-min(C(:)));
Hope this helps!
emami.m
2023년 7월 25일
jon erickson
2025년 9월 18일
0 개 추천
Thanks for sharing this. For producing a single, standalone figure this works well. However, in the case of wishing to compare two correlation matrices (say a before and after intervention), the color indexing can vary wildly depending on the nature of the data. So the use case is limited in this sense.
댓글 수: 1
Mathieu NOE
2025년 9월 19일
hello
why woudn't it possible to compare two correlation plots ? I don't see what speaks against that
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




