Shade area between a curve and a vertical line and the x axis
29 views (last 30 days)
Show older comments
I have the following code:
clc
clear
close all
x = [0:.001:.2];
y = normpdf(x,.1,.02);
subplot(211)
plot(x,y,'linewidth',2);grid;box on;
hold on
YL = get(gca, 'ylim');
YR = YL(2) - YL(1);
YL = [YL(1) - 1000 * YR, YL(2) + 1000 * YR];
line([.14 .14], YL, 'YLimInclude', 'off', 'linewidth', 2, 'Color', ...
'black','LineStyle','-.');
I want to shade the area shown below:
2 Comments
Emmanuel Sarpong
on 25 Aug 2023
I have the code below. I want to color or patch the area between the curves and wavelength (x- axis) from 3 - 5 um. I am not sure how to properly define the y limit for the patch so the patch can touch the curves. 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
Accepted Answer
Star Strider
on 29 May 2020
Add these lines:
xa = linspace(0.14, 0.2, 25); % Define ‘x’ For ‘patch’
ya = normpdf(xa,.1,.02); % Define ‘y’ For ‘patch’
patch([xa fliplr(xa)], [zeros(size(ya)) fliplr(ya)], 'r')
so the compllete revised code is now:
x = [0:.001:.2];
y = normpdf(x,.1,.02);
subplot(211)
plot(x,y,'linewidth',2);grid;box on;
hold on
YL = get(gca, 'ylim');
YR = YL(2) - YL(1);
YL = [YL(1) - 1000 * YR, YL(2) + 1000 * YR];
line([.14 .14], YL, 'YLimInclude', 'off', 'linewidth', 2, 'Color', ...
'black','LineStyle','-.');
xa = linspace(0.14, 0.2, 25); % Define ‘x’ For ‘patch’
ya = normpdf(xa,.1,.02); % Define ‘y’ For ‘patch’
patch([xa fliplr(xa)], [zeros(size(ya)) fliplr(ya)], 'r')
hold off
That should do what you want.
0 Comments
More Answers (1)
Image Analyst
on 29 May 2020
This is a FAQ: FAQ How_do_I_shade_the_region_between_two_curves?
2 Comments
Image Analyst
on 29 May 2020
Actually it did. You accepted Star's answer and you'll notice he used patch(), just like the FAQ did. You just needed to figure out the boundaries of the patch, which he kindly did for you.
See Also
Categories
Find more on Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!