Bivariate colorscale for color plot

7 ビュー (過去 30 日間)
Paul Malinowski
Paul Malinowski 2025 年 4 月 10 日
コメント済み: Mathieu NOE 2025 年 7 月 4 日
Is there a way to have a bivariate colorscale for a 2D colorplot? Meaning, lets say I have two sets of data that both depend on the same 2 variables, e.g. f(x,y) and g(x,y). Is there a way to make a 2D color map where the color at point (x,y) depends on both the values of f(x, y) and g(x, y), where I have constructed a 2D colormap in the f,g space?
  1 件のコメント
DGM
DGM 2025 年 4 月 11 日
編集済み: DGM 2025 年 4 月 11 日

サインインしてコメントする。

回答 (2 件)

Mathieu NOE
Mathieu NOE 2025 年 4 月 11 日
hello
maybe this ?
I simply assumed for the demo that f and g would be describing a circle in a 2D space
the second plot is just a check of my code , but I guess the final goal is not to use f,g but x,y instead (simple change, see commented lines)
%% create f,g functions
dx = 1/8;
N = 100;
x = linspace(-pi/2,pi/2,N);
y = linspace(-pi/2,pi/2,N);
zf = sin(x+y);% function f
zg = cos(x+y);% function g
%% create 2D colormap
% https://fr.mathworks.com/matlabcentral/answers/588934-how-can-i-make-a-2d-color-map
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));
[m,n,p] = size(I);
%% find row, col indexes of I mapping f and g
% f : x axis
% g : y axis
xx = linspace(min(zf),max(zf),m);
yy = linspace(min(zg),max(zg),n);
for k = 1:numel(zf)
[val,xxi(k)] = min(abs(zf(k)-xx));
end
for k = 1:numel(zg)
[val,yyi(k)] = min(abs(zg(k)-yy));
end
figure
axis square
image(I)
set(gca,'YDir','normal');
hold on
plot(xxi,yyi);
ind = 1:3:N-3;
for cc = 1:numel(ind)
text(xxi(ind(cc)),yyi(ind(cc)),num2str(ind(cc)));
end
hold off
figure
grid on
hold on
for k = 1:N
c(k,:) = squeeze(I(yyi(k),xxi(k),:));
% h = plot(x(k),y(k),'o');
h = plot(zf(k),zg(k),'o');
h.Color = c(k,:);
end
for cc = 1:numel(ind)
% text(x(ind(cc)),y(ind(cc)),num2str(ind(cc)));
text(zf(ind(cc)),zg(ind(cc)),num2str(ind(cc)));
end
  2 件のコメント
William Rose
William Rose 2025 年 4 月 11 日
Mathieu NOE
Mathieu NOE 2025 年 7 月 4 日
problem solved ?

サインインしてコメントする。


William Rose
William Rose 2025 年 4 月 11 日
Another approach is to map f in one color and map g in another color, then combine them.
x=-10:0.5:10; y=-10:0.5:10;
[X,Y]=meshgrid(x,y);
f=10*exp(-(X.^2+Y.^2)/32);
g=(X+Y)/10;
% Color values for f (range from 0 to 1)
cf=(f-min(f,[],"all"))/(max(f,[],"all")-min(f,[],"all"));
% Color values for g (range from 0 to 1)
cg=(g-min(g,[],"all"))/(max(g,[],"all")-min(g,[],"all"));
Cf=cat(3,cf,zeros(size(X)),zeros(size(X))); % red colors for f
Cg=cat(3,zeros(size(X)),cg,zeros(size(X))); % green colors for g
% Plot f in red and g in green
subplot(2,2,1); surf(X,Y,f,Cf,'EdgeColor','none');
xlabel('X'); ylabel('Y'); title('f(x,y)'); grid on
subplot(2,2,2); surf(X,Y,g,Cg,'EdgeColor','none');
xlabel('X'); ylabel('Y'); title('g(x,y)'); grid on
% PLot a flat surface with colors based on f an g
Cfg=cat(3,cf,cg,zeros(size(X)));
subplot(2,3,5); surf(X,Y,ones(size(X)),Cfg,'EdgeColor','none');
xlabel('X'); ylabel('Y'); title('f(x,y) and g(x,y)'); grid on

カテゴリ

Help Center および File ExchangeColor and Styling についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by