MATLAB Answers

Fill three areas in a plot with one line

1 view (last 30 days)
leonidas86 on 11 Jul 2018
Answered: Star Strider on 11 Jul 2018
Hello, I have one vector with my x axis values, one vector with the y axis values for my line and a min and max value for the y limits of my figure. Now I want to fill the area under the x-axis with one color, the area between the x axis and the line with one color and the area between the line until to my max value with another color. How can I do this?
Thanks for your help!

Answers (1)

Star Strider
Star Strider on 11 Jul 2018
I am not certain what you want.
Try this:
x = sort(rand(1, 20));
y = rand(1, 20) + 0.5;
ylow = 0.3; % Low Y-Limit
yhigh = 1.8; % High Y-Limit
x1 = ones(size(x));
patch([x fliplr(x)], [x1*ylow fliplr(y)], 'g')
hold on
patch([x fliplr(x)], [x1*yhigh fliplr(y)], 'r')
patch([x fliplr(x)], [x1*min(ylim) x1*ylow], 'b')
hold off
ylim = [ylow yhigh];

Community Treasure Hunt

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

Start Hunting!