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 comentarios

Dyuman Joshi
Dyuman Joshi el 2 de Abr. de 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.
Felix Bechtle
Felix Bechtle el 4 de Ag. de 2024
I wanted to colour each species data point the same colour.
I've looked through the corrplot function documentation and haven't really found a good way for me to do this.

Iniciar sesión para comentar.

Respuestas (1)

Voss
Voss el 2 de Abr. de 2024
Editada: Voss el 2 de Abr. de 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 comentarios

Felix Bechtle
Felix Bechtle el 4 de Ag. de 2024
Movida: Voss el 4 de Ag. de 2024
Thanks, that's basically what I was looking for.
I still have a couple of questions.
  1. Can I change the points as filled dots/circles in the corrplot function?
  2. Can I create the legend for the categories/groups/species directly in the code, the location doesn't really matter?
  3. 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?
Question 1:
Question 2:
Question 3:
Thanks
Umar
Umar el 4 de Ag. de 2024
Editada: Walter Roberson el 5 de Ag. de 2024
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
Umar el 5 de Ag. de 2024
Hi @Voss,
Thanks for clarifying and your help, really appreciated. I am crossing my fingers that OP accepts your answer.
Hello guys,
Thank you for your help and contribution to my case.
Question 1: Is solved with the following lines and the .MarkerFaceColor property.
old_line.MarkerFaceColor = old_line.Color;
new_line_1.MarkerFaceColor = new_line_1.Color;
new_line_2.MarkerFaceColor = new_line_2.Color;
Question 2: I tried using the following and the legend hasn´t worked for me.
legend('Species 1', 'Species 2', 'Species 3', 'Location', 'best');
Did not work.
legend('Species 1');
Did not work.
legend(show);
Adding this to each did not work either. Both gave me a "Warning: Ignoring extra legend entries." and in the plot no legend was inserted.
Question 3: Unfortunately, the multiple labes didn't work so well. I only managed to get one in the middle like this:
Using the x/yticklabels didn't result in a warning or an error, but it didn't affect the figure:
Once more thank you and have a nice day.
Felix
Umar
Umar el 5 de Ag. de 2024
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.
Umar
Umar el 5 de Ag. de 2024
Hi @ Felix Bechtle,
Is it possible for you to share code on this form, I will appreciate that.
Voss
Voss el 5 de Ag. de 2024
Editada: Voss el 5 de Ag. de 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')
Felix Bechtle
Felix Bechtle el 17 de Ag. de 2024
Thanks @Voss, @Umar.
With the last two hints i was able to solve all my points.
Felix

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2023a

Etiquetas

Preguntada:

el 2 de Abr. de 2024

Comentada:

el 17 de Ag. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by