How to color values in corrplot function?
古いコメントを表示
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 件のコメント
Dyuman Joshi
2024 年 4 月 2 日
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.
Felix Bechtle
2024 年 8 月 4 日
回答 (1 件)
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 件のコメント
Felix Bechtle
2024 年 8 月 4 日
移動済み: Voss
2024 年 8 月 4 日
Umar
2024 年 8 月 4 日
編集済み: Walter Roberson
2024 年 8 月 5 日
Hi @Felix Bechtle,
Please see my response to your questions below.
Can I change the points as filled dots/circles in the corrplot function?
To change the points to filled dots/circles in a corrplot, you can modify the marker style using the Marker property. Here's how you can achieve this:
old_line = h(ii, jj);
new_line_1 = copyobj(old_line, old_line.Parent);
new_line_2 = copyobj(old_line, old_line.Parent);
% Change marker style to filled circles
set(old_line, 'Marker', 'o', 'MarkerIndices', idx1)
set(new_line_1, 'Marker', 'o', 'MarkerIndices', idx2,
'Color', [0.85 0.325 0.098])
set(new_line_2, 'Marker', 'o', 'MarkerIndices', idx3, 'Color', [0.929 0.694 0.125])
Can I create the legend for the categories/groups/species directly in the code, the location doesn't really matter?
To create a legend for the categories/groups/species directly in the code, you can use the legend function. You can specify the labels for each group and customize the location of the legend as needed. Here's an example:
% Create legend for species
legend('Species 1', 'Species 2', 'Species 3', 'Location', 'best')
Also, is it possible to format the labels on the axes as if you were using the table as data input for the function and viewing the table?
If you want to format the labels on the axes as if using a table as data input, you can set the XTickLabel and YTickLabel properties of the plot. Here's how you can format the labels:
% Example of formatting X-axis labels
xticklabels({'Label 1', 'Label 2', 'Label 3', 'Label 4'})
% Example of formatting Y-axis labels
yticklabels({'Label A', 'Label B', 'Label C', 'Label D'})
Hope this answers all your questions.
@Umar: Actually the line markers are already 'o'.
@Felix Bechtle: To fill in the markers, specify the 'MarkerFaceColor' property.
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])
old_line.MarkerFaceColor = old_line.Color;
new_line_1.MarkerFaceColor = new_line_1.Color;
new_line_2.MarkerFaceColor = new_line_2.Color;
end
end
Umar
2024 年 8 月 5 日
Hi @Voss,
Thanks for clarifying and your help, really appreciated. I am crossing my fingers that OP accepts your answer.
Felix Bechtle
2024 年 8 月 5 日
Umar
2024 年 8 月 5 日
Hi @ Felix Bechtle,
It was my mistake for not testing these functions in code and jump to conclusions right away. However, I am willing to a look at your code and help fix these issues for you free of charge if you like 👍, let me know.
Felix Bechtle
2024 年 8 月 5 日
Umar
2024 年 8 月 5 日
Hi @ Felix Bechtle,
Is it possible for you to share code on this form, I will appreciate that.
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')
Felix Bechtle
2024 年 8 月 17 日
カテゴリ
ヘルプ センター および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









