How to color or patch the region between a curve and the x axis

6 views (last 30 days)
My code plots two curves. I want to color or in other words patch the region between the curves and the x-axis from 3-5 um. I am not sure how to properly define the y patch so my patch doesn't fill up the correct region of interest. Any form of help would be appreciated. Thank you.
clc;
close all;
c=3*10^8; % speed of light in vaccum
h=6.626*10.^-34; % Planck constant
k=1.38*10.^-23; % Boltzmann constant
T=[700 900]; % Temperatures in Kelvin
Lam=(0.0:0.01:50)*1e-6; % in meters
figure(1)
for i=1:2
A2(:,i)=(2*pi*h*c*c)./((Lam.^5).*(exp((h.*c)./(k.*T(i).*Lam))-1));
hold on
plot(Lam*1e6,A2(:,i)*1e-10,'r','linewidth',2)
x = [3 5]; % Define x For patch
y = [0.152 0.19]; % Define y For patch
patch([x fliplr(x)], [zeros(size(y)) fliplr(y)], 'b')
hold off
xlabel('\lambda (\mum)','fontsize',20)
ylabel('Spectral exitance (W/cm^2/\mum)','fontsize',20) %for I2
title('Blackbody Radiation','fontsize',24)
ax=gca;
ax.XRuler.Exponent=0;
xlim([0 20])
end
  2 Comments
Walter Roberson
Walter Roberson on 25 Aug 2023
Do you care about the case where some of the y values might be negative?
Emmanuel Sarpong
Emmanuel Sarpong on 25 Aug 2023
Yes I do care. I am trying to also figure out how to integrate the shaded portion under each curve (after being able to do the shading or patching properly) so I am trying to stay within the region of interest. Thanks

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 25 Aug 2023
I posted this in response to your Comment (I’ve since deleted my responding Comment), so I’m now posting it as an Answer here —
c=3*10^8; % speed of light in vaccum
h=6.626*10.^-34; % Planck constant
k=1.38*10.^-23; % Boltzmann constant
T=[700 900]; % Temperatures in Kelvin
Lam=(0.0:0.01:50).'*1e-6; % in meters
Lv = (Lam <= 5E-6) & (Lam >= 3E-6); % <— ADDED
cm = [0 0 1; 1 0 0]; % <— ADDED
figure(1)
for i=1:2
A2(:,i)=(2*pi*h*c*c)./((Lam.^5).*(exp((h.*c)./(k.*T(i).*Lam))-1));
hold on
plot(Lam*1e6,A2(:,i)*1e-10,'r','linewidth',2)
x = Lam*1e6; % Define x For patch % <— CHANGED
y(:,i) = A2(:,i)*1e-10; % Define y For patch % <— CHANGED
if i == 1
patch([x(Lv); flip(x(Lv))], [zeros(size(y(Lv,1))); flip(y(Lv,i))], cm(i,:), 'FaceAlpha',0.5, 'EdgeColor','none') % <— CHANGED
elseif i == 2
patch([x(Lv); flip(x(Lv))], [y(Lv,1); flip(y(Lv,2))], cm(i,:), 'FaceAlpha',0.5, 'EdgeColor','none') % <— CHANGED
end
hold off
xlabel('\lambda (\mum)','fontsize',20)
ylabel('Spectral exitance (W/cm^2/\mum)','fontsize',20) %for I2
title('Blackbody Radiation','fontsize',24)
ax=gca;
ax.XRuler.Exponent=0;
xlim([0 20])
end
.
  5 Comments

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!