Fill in region between data points

26 次查看(过去 30 天)

Hello all,

Trying to shade a region between my data points. The methods I've tried using don't quite work. I've tried convhull() and boundary() with a shrink factor and can't get the bounded region I'm looking for.

The lower edge of my points get lost in the boundary. See the image. I don't want the blue area in the fill.

Likewise if I had another set of points (in the red) I would want it to follow the top and bottom edge and not try to shortcut the bottom edge.

Any help would be great. Heres my code. The variables x and y are [M x N] N being each set of points on a particular curve.

 xx = reshape(x,[size(x,1).*size(x,2),1]);
 yy = reshape(y,[size(y,1).*size(y,2),1]);
 k = boundary(yy,xx,.1);
 fill(xx(k),yy(k),C(ii,1:3))
 plot(xx,yy,'ko','linewidth',2) 
 % also tried this: -----------------------
 k = convhull(x,y);
 fill(x(k),y(k),C(ii,1:3))
 plot(x,y,'ko','linewidth',2)
  2 个评论
KSSV
KSSV 2018-6-6
We need points to work on.....attach your data..
Ryan
Ryan 2018-6-6
Hi KSSV, just attached the x and y data.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2018-6-6
Try this:
figure
plot(x, y, 'o')
hold on
patch([x(:,1)' fliplr(x(:,3)')], [y(:,1)' fliplr(y(:,3)')], 'b', 'FaceAlpha',0.3)
hold off
I first ran:
[colmax,mxidx] = max(y);
to determine which columns were the upper and lower curves, and used those results in the patch call. The code then takes columns 1 and 3 of ‘x’ and ‘y’, turns them into row vectors and flips column 3. This defines the area for patch to fill, since it needs a closed area. See the documentation on patch (link) for details.
Experiment to get the result you want.
  2 个评论
Ryan
Ryan 2018-6-7
Hi Star Strider,
That works well for data that doesn't cross each other (see my second image I posted), but I think I see what I need to do. I need to find the upper and lower surface and then use patch. Thanks for the help!
Star Strider
Star Strider 2018-6-7
As always, my pleasure!
To use patch to create the second image, The idea would be to interpolate the original data to create a common grid for both, get the maxima and minima to define the limits of the patch object, and then plot those.
Example
x1 = sort(rand(1,30)); % Create Data
y1 = 1 - 5*(x1-0.5).^2; % Create Data
x2 = sort(rand(1,40)); % Create Data
y2 = sin(2*pi*x2); % Create Data
xi = linspace(min([x1(:); x2(:)]), max([x1(:); x2(:)]), 50); % Interpolation Vector
y1i = interp1(x1, y1, xi, 'linear','extrap'); % Interpolate ‘y1’
y2i = interp1(x2, y2, xi, 'linear','extrap'); % Interpolate ‘y2’
upper = max([y1i; y2i]); % Upper Limit
lower = min([y1i; y2i]); % Lower Limit
figure
plot(x1, y1, '-p', x2, y2, '-p')
hold on
patch([xi fliplr(xi)], [upper fliplr(lower)], [0.5 0.2 0.1], 'FaceAlpha',0.2)
hold off
You will have to experiment with this approach with your own data.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by