How can I make a 2D color map?

460 views (last 30 days)
Richard Ott
Richard Ott on 4 Sep 2020
Commented: Bruno Luong on 4 Sep 2020
Hi everyone,
I want to color data in a plot by using 2 color coordinates to achieve basically a 2D colormap.
Here's a link to the kind of coloramps I'm talking about https://dominikjaeckle.com/projects/color2d/
I couldn't find anything for Matlab on how to create such a coloarmap. Any ideas?
Thanks so much in advance.
Cheers
Richard
  2 Comments
Richard Ott
Richard Ott on 4 Sep 2020
No, the link you send is for a normal 1D color map. You specify one value and that's what it's colored by.
What I mean is a colormap with gradients in two direction as specified by a value pair (as shown in the link that I posted).

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 4 Sep 2020
Edited: Bruno Luong on 4 Sep 2020
Something like this
R=[1 0;
1 0];
G=[1 1
0 0];
B=[0 0
0 1];
R = interp2(R,8);
G = interp2(G,8);
B = interp2(B,8);
I = uint8(255*cat(3,R,G,B));
image(I)

More Answers (1)

Image Analyst
Image Analyst on 4 Sep 2020
Try this. The code creates an RGB image to use as a colormap. Then it creates a scatterplot and uses the RGB image to decide the colors of the markers:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%==================================================================================================
% Show lab color space for values of L = 50.
numColors = 256;
ramp = linspace(-100,100, numColors);
figure;
cform = makecform('lab2srgb');
a = repmat(ramp, [numColors 1]); % -a on left
b = repmat(flipud(ramp'), [1 numColors]); % -b on bottom
L = 50 * ones(numColors, numColors); % A single L value.
Lab = cat(3, L, a, b); % A 2D image.
colormap2D = applycform(Lab, cform);
% Display it.
subplot(2, 1, 1);
imshow(colormap2D);
axis on;
caption = sprintf('2D Colormap');
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Now that RGB image is made up (to be a 2-D colormap), make scatterplot.
numPoints = 1000;
amplitude = 3000;
x = amplitude * rand(1, numPoints);
y = amplitude * rand(1, numPoints);
subplot(2, 1, 2);
grid on;
thisColor = zeros(numPoints, 3);
for k = 1 : numPoints
col = ceil(x(k) * numColors / amplitude);
row = ceil(y(k) * numColors / amplitude);
thisColor(k, :) = [colormap2D(row, col, 1), colormap2D(row, col, 2), colormap2D(row, col, 3)];
fprintf('(x,y) = (%6.1f, %6.1f), row = %3d, col = %3d, thisColor = (%.4f, %.4f, %.4f)\n', x(k), y(k), row, col, thisColor(k, :));
% plot(x(k), y(k), '.', 'Color', thisColor, 'MarkerSize', 30);
% hold on;
end
scatter(x, y, 30 * ones(1, numPoints), thisColor, 'filled');
axis('square');
grid on;
xlim([0, amplitude]);
Of course you could adapt it to use any RGB image as a colormap.
  3 Comments
Image Analyst
Image Analyst on 4 Sep 2020
Edited: Image Analyst on 4 Sep 2020
Your question asked 2 things
  1. How to create a 2-D colormap, and
  2. How to plot markers using colors from that colormap.
I thought I'd better add the part where I gave a scatterplot since you asked for that but the answer you accepted did not have that.
I don't know what you mean by the main colors. If you want to rotate the image, you can do
colormap2D = imrotate(colormap2D, 45);
but like I said, the scatterplot code where I make up the set of colored markers can use any image whatsoever. Just make it up however you want. To drive home that point, here is a demo where I use the peppers demo image as the colormap:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%==================================================================================================
colormap2D = imread('peppers.png');
[numColorsY, numColorsX, numberOfColorChannels] = size(colormap2D)
% Display it.
subplot(2, 1, 1);
imshow(colormap2D);
axis on;
caption = sprintf('2D Colormap');
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Divide by image colormap 255 to get into the range 0-1. It's originally 0-255 which is not the right range for a colormap.
colormap2D = double(colormap2D) / 255;
% Now that RGB image is made up (to be a 2-D colormap), make scatterplot.
numPoints = 1000;
amplitude = 3000;
x = amplitude * rand(1, numPoints);
y = amplitude * rand(1, numPoints);
subplot(2, 1, 2);
grid on;
thisColor = zeros(numPoints, 3);
for k = 1 : numPoints
col = ceil(x(k) * numColorsX / amplitude);
row = ceil(y(k) * numColorsY / amplitude);
thisColor(k, :) = double([colormap2D(row, col, 1), colormap2D(row, col, 2), colormap2D(row, col, 3)]);
fprintf('(x,y) = (%6.1f, %6.1f), row = %3d, col = %3d, thisColor = [%.4f, %.4f, %.4f]\n', x(k), y(k), row, col, thisColor(k, :));
% plot(x(k), y(k), '.', 'Color', thisColor, 'MarkerSize', 30);
% hold on;
end
scatter(x, y, 30 * ones(1, numPoints), thisColor, 'filled');
axis('square');
grid on;
xlim([0, amplitude]);
title('scatterplot', 'FontSize', fontSize);
If this is helpful, can you Vote for my Answer? Thanks in advance.
Bruno Luong
Bruno Luong on 4 Sep 2020
"In both answer the gradients start in the corner. I'm trying to think if there's a way to have the main colors on the middle of the side-edgdes. Basically, a 45° rotation of this colormap."
There is a reason for that.
If you define the colors at 4 corners, color inside is an interpolation, because all points inside the rectangle is the barycentric combination of the four corners.
If you define the colors at 4 midlles points of the edges, this is no longer true, and you need to extrapolate to get to the corner. The extrapolation results can be unexpected (strange colors, overflow RGB range, etc...)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!