Fancy Correlation Plots in MATLAB
Show older comments
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 Comment
Mathieu NOE
on 22 Dec 2020
hi
you should be able to get this by combining
corrcoef
and
Accepted Answer
More Answers (2)
8 Comments
Mathieu NOE
on 23 Dec 2020
excellent work !!
Adam Danz
on 23 Dec 2020
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
on 23 Dec 2020
.... and remove / comment axis off
otherwise you lose all axis labeling, tick marks and background
emami.m
on 23 Dec 2020
Mathieu NOE
on 24 Dec 2020
you're welcome !
Martin Vallejos
on 1 May 2021
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
on 25 Jul 2023
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
on 25 Jul 2023
jon erickson
on 18 Sep 2025
0 votes
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 Comment
Mathieu NOE
on 19 Sep 2025
hello
why woudn't it possible to compare two correlation plots ? I don't see what speaks against that
Categories
Find more on Line Plots 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!



