Azzera filtri
Azzera filtri

How to apply a 'custom' colormap to multiple data sets including negative and positive values

4 visualizzazioni (ultimi 30 giorni)
Hi everyone,
Got an issue I've been working on for the last few weeks and cannot figure it out.
I got several datasets that I plot individually across the x-axis. I want to apply the colormap to the filled plot dots to reflect the colormap (ie negative data plots are blue, positive are red and anything around 0 is white/pale).
My problem, I have no idea how to apply the colormap to the plotted data.
Is there possibly a way to extract the RGB values between 0 - 256 to reflect the plot colours? so I can just add an array of RGB values to the plot code, using the example here: https://uk.mathworks.com/help/matlab/ref/scatter.html under the subtitle "Fill the markers" (im referring to c = linspace(1,10,length(x));)
here is example code:
figure
sz = 25;
set(gcf,'Color','w') % set the background of figure to white
set1=rand(20,1)
set2=rand(20,1)*-4
set3=rand(20,1)*6
all_x_data(1:20)=1 %each set plotted at one x coordinate
scatter(all_x_data,set1,sz,'filled')
hold on
scatter(all_x_data+1,set2,sz,'filled')
scatter(all_x_data+2,set3,sz,'filled')
xlim([0, 4]) % change the axes limits
ylim([-6 6])
yline(0,'--k') % add a dot line at y=0
% open colormapeditor
% change the axis to -6 and 6
% change the colours at RBG 1;blue, 256;red and 127;white

Risposta accettata

Walter Roberson
Walter Roberson il 8 Nov 2023
You indicated that you wanted the values "around" 0 to be white. In the following code, the values between -1/2 and +1/2 are mapped to white.
However... white markers on a white background is barely visible, so it is hard to tell.
figure
sz = 25;
set(gcf,'Color','w') % set the background of figure to white
set1=rand(20,1);
set2=rand(20,1)*-4;
set3=rand(20,1)*6;
all_x_data(1:20)=1; %each set plotted at one x coordinate
cmap = zeros(25, 3);
cmap(1:12,:) = repmat([0 0 .9], 12, 1); %blue
cmap(13,:) = [.9 .9 .9]; %white
cmap(14:25,:) = repmat([0.9 0 0], 12, 1); %red
disp(cmap)
0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0.9000 0.9000 0.9000 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0 0.9000 0 0
scatter(all_x_data,set1,sz,set1,'filled')
hold on
scatter(all_x_data+1,set2,sz,set2, 'filled')
scatter(all_x_data+2,set3,sz,set3, 'filled')
xlim([0, 4]) % change the axes limits
ylim([-6 6])
yline(0,'--k') % add a dot line at y=0
caxis([-6 6]);
colormap(cmap);
  2 Commenti
Walter Roberson
Walter Roberson il 8 Nov 2023
figure
sz = 25;
set(gcf,'Color','w') % set the background of figure to white
set1=rand(20,1);
set2=rand(20,1)*-4;
set3=rand(20,1)*6;
all_x_data(1:20)=1; %each set plotted at one x coordinate
ramp = linspace(0.1, .9, 12);
cmap = zeros(25, 3);
cmap(1:12,1) = ramp;
cmap(1:12,2) = ramp;
cmap(1:12,3) = 0.9; %blue
cmap(13,:) = [.9 .9 .9]; %white
cmap(14:25,1) = 0.9; %red
cmap(14:25,2) = fliplr(ramp);
cmap(14:25,3) = fliplr(ramp);
disp(cmap)
0.1000 0.1000 0.9000 0.1727 0.1727 0.9000 0.2455 0.2455 0.9000 0.3182 0.3182 0.9000 0.3909 0.3909 0.9000 0.4636 0.4636 0.9000 0.5364 0.5364 0.9000 0.6091 0.6091 0.9000 0.6818 0.6818 0.9000 0.7545 0.7545 0.9000 0.8273 0.8273 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.8273 0.8273 0.9000 0.7545 0.7545 0.9000 0.6818 0.6818 0.9000 0.6091 0.6091 0.9000 0.5364 0.5364 0.9000 0.4636 0.4636 0.9000 0.3909 0.3909 0.9000 0.3182 0.3182 0.9000 0.2455 0.2455 0.9000 0.1727 0.1727 0.9000 0.1000 0.1000
scatter(all_x_data,set1,sz,set1,'filled')
hold on
scatter(all_x_data+1,set2,sz,set2, 'filled')
scatter(all_x_data+2,set3,sz,set3, 'filled')
xlim([0, 4]) % change the axes limits
ylim([-6 6])
yline(0,'--k') % add a dot line at y=0
caxis([-6 6]);
colormap(cmap);
colorbar()
Vyte Jan
Vyte Jan il 8 Nov 2023
Thank you for your reply.
Your second suggestion was a lot closer to the vision I had for this figure.
Please see below the figure using my actual data that I was able to create using your suggestion, looks awesoome!
I changed the linspace from 12 to 125 to emphasize the colour change. I should have specified the purpose of me using the colourbar instead of a single colour (ie blue/red), was to get a gradient representation for the plotted data. I will also be using the same gradient effect on a brain map to emphasize spatial information in 3D space.
Thank you very much for your help on this.

Accedi per commentare.

Più risposte (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 8 Nov 2023
As understood your question, this is what you are trying to get:
figure
set(gcf,'Color','w') % set the background of figure to white
set1=randn(20,1);
set2=randn(20,1)*-4;
set3=randn(20,1)*6;
all_x_data(1:20)=1; %each set plotted at one x coordinate
C = linspace(1,6,20); % Color space
scatter(all_x_data, sort(set1),[],C,'filled')
hold on
scatter(all_x_data+1,sort(set2),[],C,'filled')
scatter(all_x_data+2,sort(set3),[],C,'filled')
xlim([0, 4]) % change the axes limits
ylim([-6 6])
yline(0,'--k') % add a dot line at y=0
colorbar
colororder('reef')
  1 Commento
Walter Roberson
Walter Roberson il 8 Nov 2023
The user wants all negative to be blue, all positive to be red, and "near zero" to be white.
It is not immediately clear if the user wants "more negative to be darker blue"

Accedi per commentare.

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by