Overlaying gradient patches in single plot.

3 次查看(过去 30 天)
I have created two gradient patches: the colormap for one goes from red to white (i.e., [1,0,0] --> [1, 1, 1]) over the range [1000,1600] along the x-axis of a subplot, while the other colormap goes from white to blue (i.e., [1, 1, 1] --> [0, 0, 1]/) over the range [1400, 2000] along the x-axis of a second subplot. xlim for both suplots is [1000, 2000], and I use a for loop with 256 iterations to create the colormaps. Is there any way to superimpose the two patches in one x-y plot (adjusting transparency so that both gradients are visible where the colored portions of the two patches overlap (i.e., x = 1400 --> 1600)? Thanks in advance.
  3 个评论
Eric
Eric 2021-6-1
function varargout = Tamb_vs_Altitude_gradient()
clc; close all;
x_lo = [1000 ; 1000; 1600; 1600]; % x-limits for RED gradient
x_hi = [1400 ; 1400; 2000; 2000]; % x-limits for BLUE gradient
y = [0; 500; 500; 0]; % y-limits for BOTH gradients
c = [0; 0; 1; 1];
num_steps = 256;
for cm = 1:num_steps % Create the colormaps
Ag_colormap_lo(cm,:) = [255, 0 + cm*(255-0)/num_steps, 0 + cm*(255-0)/num_steps]/255;
Ag_colormap_hi(cm,:) = [255 - cm*(255-0)/num_steps, 255 - cm*(255-0)/num_steps, 255]/255;
end
save('Tamb_gradient_colormap.mat','Ag_colormap_lo','Ag_colormap_hi');
figure();
ax(1) = subplot(2,1,1); % Plot the RED gradient
p_lo = patch(x_lo,y,c);
set(p_lo,'FaceAlpha',1) % Adjust transparency of RED gradient
shading interp;
colorbar;
xlim([1000 2000]); ylim([0 500]);
ax(2) = subplot(2,1,2); % Plot the BLUE gradient
p_hi = patch(x_hi,y,c);
set(p_hi,'FaceAlpha',1) % Adjust transparency of BLUE gradient
shading interp;
colorbar;
xlim([1000 2000]); ylim([0 500]);
colormap(ax(1),Ag_colormap_lo); % Assign RED colormap to RED gradient
colormap(ax(2),Ag_colormap_hi); % Assign BLUE colormap to BLUE gradient
end
%%
Eric
Eric 2021-6-1
Here're the gradients I wish to superimpose on the one axis.

请先登录,再进行评论。

采纳的回答

darova
darova 2021-6-5
编辑:darova 2021-6-5
See this
% x_lo = [1000 ; 1000; 1600; 1600]; % x-limits for RED gradient
% x_hi = [1400 ; 1400; 2000; 2000]; % x-limits for BLUE gradient
% y = [0; 500; 500; 0]; % y-limits for BOTH gradients
num_steps = 10;
x = [linspace(1000,1400,num_steps) linspace(1600,2000,num_steps)]; % x coord
X = [x;x]; % the same top and bottom
Y = [0*x;0*x+500]; % Y coord
c = linspace(0,1,num_steps); % color interpolatioon
red = interp1([0 1],[1 0 0;1 1 1],c); % create red chanel
blu = interp1([0 1],[1 1 1;0 0 1],c); % create blue chanel
C = reshape([red;blu],1,[],3); % 3D matrix of colors
C = [C;C]; % the same top and bottom
surf(X,Y,X*0,C)
view(0,90)
  2 个评论
Eric
Eric 2021-6-5
Hello, Darova -
That's good stuff, but not quite what I was hoping would be possible, which would be the two gradients overlapping in the range of x = [1400 1600], producing a 'zone' that would go from red (RGB = [255, 0, 0] near 1400) to blue (RGB = [0, 0, 255] near 1600), and progressing from red through various shades of violet in between.
However, THANKS TO YOUR ANSWER, I learned enough to be able to produce exactly what I wanted. Couldn't have done it without your help. A gazillion thanks to you!!!
Eric

请先登录,再进行评论。

更多回答(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