2

I'm trying to fill the areas between a polyline and a horizontal line (that cut this polyline in several points) using Octave/Matlab, but I want to keep only the areas below the horizontal line.

This is what I've come up so far:

x = 0:0.5:6;
y = [3 2.5 1 1 1 2.5 3 2.5 1 1 1 2.5 3];
yline(1:13) = 2;

figure(1)
plot(x,y,'k')

fill([x fliplr(x)],[y yline],'g')
axis equal
xlim([-1 7]);

I searched for days to find a solution but I only went close to the answer here, here and here (unfortunately this last one is only for r-code).

Community
  • 1
  • 1
Paul
  • 23
  • 2

1 Answers1

3

You can use the following trick:

  1. Fill normally, as you do in your code. No need for coloring the edges; it will be done later.
  2. Draw a white rectangular patch to cover the part you don't want filled. No edge color here either.
  3. Plot the lines on top of that.

Code:

x = 0:0.5:6;
y = [3 2.5 1 1 1 2.5 3 2.5 1 1 1 2.5 3];
yline(1:13) = 2;

figure(1)

fill([x fliplr(x)],[y yline],'g', 'edgecolor', 'none')
hold on
patch([min(x) max(x) max(x) min(x)],[yline(1) yline(1) max(y) max(y)], 'w', ...
    'edgecolor', 'none')
plot(x,y,'k')
plot(x,yline,'k')
axis equal
xlim([-1 7]);

Resulting figure:

enter image description here

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147