How to color values in corrplot function?
8 ビュー (過去 30 日間)
古いコメントを表示
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.
回答 (1 件)
Voss
2024 年 4 月 2 日
編集済み: Voss
2024 年 4 月 2 日
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 件のコメント
Voss
2024 年 8 月 5 日
編集済み: Voss
2024 年 8 月 5 日
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')
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









