Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
Emmanuel Sarpong
Emmanuel Sarpong le 25 Août 2023
Commenté : Star Strider le 25 Août 2023
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 commentaires
Walter Roberson
Walter Roberson le 25 Août 2023
Do you care about the case where some of the y values might be negative?
Emmanuel Sarpong
Emmanuel Sarpong le 25 Août 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

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 25 Août 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 commentaires
Emmanuel Sarpong
Emmanuel Sarpong le 25 Août 2023
All is perfect now. Adding the clearvars did it. Thanks a lot
Star Strider
Star Strider le 25 Août 2023
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by