How to color values in corrplot function?
8 views (last 30 days)
Show older comments
Hello,
I created a corrplot with the fisheriris dataset.

I wanted to add a 3rd dimension in the corrplot coloring the values to the corresponding species, like in the following plot.

Is this possible in Matlab?
Thanks for the help and all the best!
2 Comments
Dyuman Joshi
on 2 Apr 2024
It's not clear to me how you want to group values in the corrplot.
Although, you can get the handle to the graphics objects present in a corrplot() and modify the color (and other properties) of the objects accordingly.
Answers (1)
Voss
on 2 Apr 2024
Edited: Voss
on 2 Apr 2024
Here's one way:
load fisheriris
[~,~,h] = corrplot(meas);
g = findgroups(species);
idx1 = find(g == 1);
idx2 = find(g == 2);
idx3 = find(g == 3);
for ii = 1:4
for jj = 1:4
if ii == jj
continue
end
old_line = h(ii,jj);
new_line_1 = copyobj(old_line,old_line.Parent);
new_line_2 = copyobj(old_line,old_line.Parent);
set(old_line,'MarkerIndices',idx1)
set(new_line_1,'MarkerIndices',idx2,'Color',[0.85 0.325 0.098])
set(new_line_2,'MarkerIndices',idx3,'Color',[0.929 0.694 0.125])
end
end
10 Comments
Voss
on 5 Aug 2024
Edited: Voss
on 5 Aug 2024
For question 3 (axis labels), you can try specifying the variable names in corrplot directly:
load fisheriris
[~,~,h] = corrplot(meas,'VarNames',{'SepalLength','SepalWidth','PetalLength','PetalWidth'});
However, the result looks like one of your previous screen shots, and I'm not sure why the labels are cut off after 5 characters. Increasing the figure size doesn't help:
fpos = get(gcf,'Position');
fpos([3 4]) = [1000 1000];
set(gcf,'Position',fpos)
For question 2 (legend), you need to specify the lines/objects that the legend should contain (if you omit this, legend uses the objects in the current axes, which is the last histogram plot - obviously not the right lines to use in the legend). In this case, since you say it doesn't matter where the legend is, then it doesn't matter which lines you use as long as you have one blue one, one red one, and one yellow one (in other words, they can be from any axes that's not on the diagonal). For convenience, you can use lines in the axes in row 4 column 3, which is the last one processed in the loops. In other words, just using the final value of old_line, new_line_1, and new_line_2 should work sufficiently (and I use the 'Location','best' option to try to avoid the legend overlapping the data):
load fisheriris
[~,~,h] = corrplot(meas,'VarNames',{'SepalLength','SepalWidth','PetalLength','PetalWidth'});
g = findgroups(species);
idx1 = find(g == 1);
idx2 = find(g == 2);
idx3 = find(g == 3);
for ii = 1:4
for jj = 1:4
if ii == jj
continue
end
old_line = h(ii,jj);
new_line_1 = copyobj(old_line,old_line.Parent);
new_line_2 = copyobj(old_line,old_line.Parent);
set(old_line,'MarkerIndices',idx1)
set(new_line_1,'MarkerIndices',idx2,'Color',[0.85 0.325 0.098])
set(new_line_2,'MarkerIndices',idx3,'Color',[0.929 0.694 0.125])
old_line.MarkerFaceColor = old_line.Color;
new_line_1.MarkerFaceColor = new_line_1.Color;
new_line_2.MarkerFaceColor = new_line_2.Color;
end
end
legend([old_line,new_line_1,new_line_2],{'Species 1','Species 2','Species 3'},'Location','best')
See Also
Categories
Find more on Annotations 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!








