필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
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
Walter Roberson
Walter Roberson 2023년 8월 25일
Do you care about the case where some of the y values might be negative?
Emmanuel Sarpong
Emmanuel Sarpong 2023년 8월 25일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 8월 25일
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
Emmanuel Sarpong
Emmanuel Sarpong 2023년 8월 25일
All is perfect now. Adding the clearvars did it. Thanks a lot
Star Strider
Star Strider 2023년 8월 25일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by