How to apply a 'custom' colormap to multiple data sets including negative and positive values
4 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Walter Roberson
on 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)
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 Comments
Walter Roberson
on 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)
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()
More Answers (1)
Sulaymon Eshkabilov
on 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 Comment
Walter Roberson
on 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"
See Also
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!