フィルターのクリア

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

4 ビュー (過去 30 日間)
Emmanuel Sarpong
Emmanuel Sarpong 2023 年 8 月 25 日
コメント済み: Star Strider 2023 年 8 月 25 日
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 Center および File ExchangePolygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by