Use of 2D colormaps possible in Matlab?

4 次查看(过去 30 天)
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

采纳的回答

Dave B
Dave B 2021-11-22
编辑:Dave B 2021-11-22
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 个评论
Marcus Becker
Marcus Becker 2021-11-22
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 2021-11-23
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Orange 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by