Use of 2D colormaps possible in Matlab?

4 views (last 30 days)
Is there a way to use colormaps in Matlab which change in two dimensions?
If yes, how? And how do you create them?
One example could be to have magitude and orientation of a 2D vector field in one plot.
I have found some examples on other platforms:
Or this discussion here about circular 2D colormaps:
Best regards

Accepted Answer

Dave B
Dave B on 22 Nov 2021
Edited: Dave B on 22 Nov 2021
There's nothing (that I know of) that provides this as a built-in utility in MATLAB, but it's pretty easy to do this kind of thing by interpolating indices into a colormap. For example, to recreate the scatter at the top of the link:
%% Load a colormap and store in a matrix of RGB values
im = imread('https://dominikjaeckle.com/projects/color2d/data/bremm.png');
rgb = double(reshape(im, [], 3))./255;
%% Example mapping
x=rand(100,1);
y=rand(100,1);
% interpolate to find a color for each x and y, mapping the range of the
% colormap onto [0 1]
c_column=round(interp1(linspace(0,1,size(im,2)), 1:size(im,2), x));
c_row=round(interp1(linspace(0,1,size(im,1)), 1:size(im,1), y));
rgb_row=sub2ind(size(im),c_row,c_column,ones(size(c_row))); % this is the same as the index into im of red values
c=rgb(rgb_row,:);
%% Make some plots
t=tiledlayout(1,3);
nexttile;image(im)
nexttile;scatter(x,y,'filled')
nexttile;scatter(x,y,[],c,'filled')
set(t.Children,'XTick',[],'YTick',[],'box','on','DataAspectRatio',[1 1 1],'YDir','reverse')
  4 Comments
Marcus Becker
Marcus Becker on 22 Nov 2021
For me, ideally there would be function calls like
mesh4d(x,y,u,v,ulabel,vlabel,'Cmap2d','bremm')
or
scatter4d(x,y,u,v,'Cmap2d','bremm')
which provide a selection of useful colormaps and maybe even an interface for custom ones.
Issue might be that the colorbar becomes a colorplane and needs x and y labels.
But I do think that it would be helpful to put it on file exchange!
Marcus Becker
Marcus Becker on 23 Nov 2021
I have created a Git with a working version of the function. It's not amazing code but it works for my needs:
Feel free to use, share, adapt.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!