Fancy Correlation Plots in MATLAB
Mostrar comentarios más antiguos
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 comentario
Mathieu NOE
el 22 de Dic. de 2020
hi
you should be able to get this by combining
corrcoef
and
Respuesta aceptada
Más respuestas (2)
8 comentarios
Mathieu NOE
el 23 de Dic. de 2020
excellent work !!
Adam Danz
el 23 de Dic. de 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
el 23 de Dic. de 2020
.... and remove / comment axis off
otherwise you lose all axis labeling, tick marks and background
emami.m
el 23 de Dic. de 2020
Mathieu NOE
el 24 de Dic. de 2020
you're welcome !
Martin Vallejos
el 1 de Mayo de 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
el 25 de Jul. de 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
el 25 de Jul. de 2023
jon erickson
el 18 de Sept. de 2025
0 votos
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 comentario
Mathieu NOE
el 19 de Sept. de 2025
hello
why woudn't it possible to compare two correlation plots ? I don't see what speaks against that
Categorías
Más información sobre Line Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




