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!




