Overlaying gradient patches in single plot.

1 Ansicht (letzte 30 Tage)
Eric
Eric am 29 Mai 2021
Kommentiert: darova am 5 Jun. 2021
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 Kommentare
Eric
Eric am 1 Jun. 2021
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 am 1 Jun. 2021
Here're the gradients I wish to superimpose on the one axis.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

darova
darova am 5 Jun. 2021
Bearbeitet: darova am 5 Jun. 2021
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 Kommentare
Eric
Eric am 5 Jun. 2021
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
darova
darova am 5 Jun. 2021
You are welcome

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Orange finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by