Fill in region between data points
46 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ryan
el 5 de Jun. de 2018
Comentada: Star Strider
el 7 de Jun. de 2018
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 comentarios
Respuesta aceptada
Star Strider
el 6 de Jun. de 2018
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 comentarios
Star Strider
el 7 de Jun. de 2018
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!