Linking 2D datapoint colors to colormap

I am trying to make the color of my swarm chart's data points correspond to the colormap/colorbar. I've tried functions such as imagesc(), colormap(), or just plain old color(), but none of these seem to be working.
Here are the arrays I'm working with, stored as doubles:
data1 =
0.330000000000000
0.500000000000000
0.570000000000000
0.450000000000000
0.460000000000000
0.700000000000000
0.600000000000000
-0.0900000000000000
0.400000000000000
-0.340000000000000
-0.360000000000000
-0.750000000000000
-0.0300000000000000
-0.100000000000000
grp1 =
2
2
2
2
2
2
2
2
2
3
3
3
3
3
Here is my code so far:
figure; swarmchart(grp1(1:14,1)-1, data1(1:14,1), 30, c, 'filled')
figure; swarmchart(grp1(1:14,1)-1, data1(1:14,1), 30, c, 'filled')
hold on; boxplot( data1(1:13,1),grp1(1:13,1))
hold on; plot([1.:1:3.5]-1,[0 0.45 -0.35])
hold on; plot([0 3], [0 0], '--')
c = colormap(turbo)
colorbar
clim([-0.8 0.8])
xlim([0 3])
title('Network Activity vs. Disease Progression')
xlabel('Pathological Burden')
ylabel('Electrophysiological Disturbance')
xticklabels({'Low Burden', 'High Burden'})
Using c in the swarmplot calls throws this error message:
If I comment out c, here is my output:
I've found people doing this in older MATLAB versions and their explanations are not so helpful for me, unfortunately. Would anyone be able to send an idea/solution for this? I would sincerely appreciate your effort.

 Respuesta aceptada

Angelo Yeo
Angelo Yeo el 19 de Jul. de 2023
Editada: Angelo Yeo el 19 de Jul. de 2023
data1 = [0.33; 0.5; 0.57; 0.45; 0.46; 0.7; 0.6; -0.09; 0.4; -0.34; -0.36; ...
-0.75; -0.03; -0.1];
grp1 = [2; 2; 2; 2; 2; 2; 2; 2; 2; 3; 3; 3; 3; 3];
% Set the number of colormap to match the number of points in the swarmchart below
c = colormap(turbo(length(data1)));
figure; swarmchart(grp1(1:14,1)-1, data1(1:14,1), 30, c, 'filled')
hold on; boxplot( data1(1:13,1),grp1(1:13,1))
hold on; plot([1.:1:3.5]-1,[0 0.45 -0.35])
hold on; plot([0 3], [0 0], '--')
colorbar; colormap(turbo(length(data1)))
clim([-0.8 0.8])
xlim([0 3])
title('Network Activity vs. Disease Progression')
xlabel('Pathological Burden')
ylabel('Electrophysiological Disturbance')
xticklabels({'Low Burden', 'High Burden'})

3 comentarios

Maximilian
Maximilian el 19 de Jul. de 2023
Thank you so much; this is definitely a huge improvement! However, I noticed that the negative values on the graph, which should be represented in cooler colors, are shown as warm, and vice-versa. Might you know why that is?
Oh, now I understood your intention. Would the code below work for you?
data1 = [0.33; 0.5; 0.57; 0.45; 0.46; 0.7; 0.6; -0.09; 0.4; -0.34; -0.36; ...
-0.75; -0.03; -0.1];
grp1 = [2; 2; 2; 2; 2; 2; 2; 2; 2; 3; 3; 3; 3; 3];
c = colormap(turbo(100));
% Find the closest value in the array
A = linspace(-0.8, 0.8, 100);
A = repmat(A, [length(data1), 1]);
[~, minIdx] = min(abs(A - data1), [], 2);
figure; swarmchart(grp1(1:14,1)-1, data1(1:14,1), 30, c(minIdx, :), 'filled')
hold on; boxplot( data1(1:13,1),grp1(1:13,1))
hold on; plot([1.:1:3.5]-1,[0 0.45 -0.35])
hold on; plot([0 3], [0 0], '--')
colorbar; colormap(c);
clim([-0.8 0.8])
xlim([0 3])
title('Network Activity vs. Disease Progression')
xlabel('Pathological Burden')
ylabel('Electrophysiological Disturbance')
xticklabels({'Low Burden', 'High Burden'})
Maximilian
Maximilian el 19 de Jul. de 2023
Absolutely perfect! Thank you again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2023a

Preguntada:

el 19 de Jul. de 2023

Comentada:

el 19 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by