How can I shade an area of my graph between two vertical lines?

5 views (last 30 days)
Hello, I am trying to shade an area under the curve of a lognormal pdf between two vertical lines. Here is the code to help you generate the problem:
pd = makedist('Lognormal','mu',3.323167914007358,'sigma',0.241878609884653)
r = random(pd,1000,1); % generate 1000 numbers from the distribution
ix = linspace(min(r),max(r)); % to scale the graph
y = pdf(pd, ix);
figure
plot(ix,y)
hold on;
line([20, 20], ylim, 'LineWidth', 2, 'Color', 'r');
hold on;
line([24, 24], ylim, 'LineWidth', 2, 'Color', 'r');
This is the picture of the plot above:
I tried searching, but the best answer I have come up with fills the area between the lines all the way to the top of the graph, while I want it to end at the curve. Also, I want the lines to stop at the curve, not stretch beyond it. Here is a google picture of what I am trying to achieve:

Accepted Answer

dpb
dpb on 12 Jun 2018
iz=linspace(20,24,40);
yz=pdf(pd,iz);
area(iz,yz)
  2 Comments
Edi Kosovic
Edi Kosovic on 12 Jun 2018
Edited: dpb on 12 Jun 2018
Lol, I was trying all sorts of complicated things for hours, and you solved it in 3 lines of code. Good job and thank you very much!
dpb
dpb on 12 Jun 2018
Edited: dpb on 12 Jun 2018
Oftentimes there are too many choices... :)
If you were to make your other curve x-spacing match up, you could use a subset within it for the areas rather than generating a different x vector but that's a detail that may/may not be convenient.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!