How to change size and color of only some edges of a patch?

67 views (last 30 days)
How can I change size/thikness and color of only some edges of a patch?
For example, here I would like to change size/thikness and color of non-consecutive edges:
x = [0 1 1 0];
y = [0 0 1 1];
patch(x,y,'red','FaceAlpha',0.2);

Accepted Answer

Voss
Voss on 18 Feb 2022
Edited: Voss on 18 Feb 2022
There are a couple of options for the 'EdgeColor' of a patch object that may work, e.g., use 'EdgeColor' 'flat' to have each edge's color match the color of a vertex, or 'interp' to have edge colors linearly vary along the edge according the colors at the beginning and end vertices.
However, I don't think there is a way to vary the thickness ('LineWidth') of different edges in a patch.
Therefore it may be necessary (or just easier) to set the patch's 'EdgeColor' to 'none' and create line objects on top of the patch along the patch's edges and the lines can be whatever color and thickness you want.
For example:
x = [0 1 1 0];
y = [0 0 1 1];
patch(x,y,'red','FaceAlpha',0.2,'LineStyle','none');
x_aug = x([1:end 1]);
y_aug = y([1:end 1]);
for ii = 1:numel(x_aug)-1
line(x_aug(ii+[0 1]),y_aug(ii+[0 1]),'Color',rand(1,3),'LineWidth',randi(5));
end
You could even have many different lines running along an edge, one after the other, each with different properties:
figure();
x = [0 1 1 0];
y = [0 0 1 1];
patch(x,y,'red','FaceAlpha',0.2,'LineStyle','none');
x_aug = x([1:end 1]);
y_aug = y([1:end 1]);
N_segments = [1 5 10 20];
for ii = 1:numel(x_aug)-1
x_temp = linspace(x_aug(ii),x_aug(ii+1),N_segments(ii)+1);
y_temp = linspace(y_aug(ii),y_aug(ii+1),N_segments(ii)+1);
for jj = 1:N_segments(ii)
line(x_temp(jj+[0 1]),y_temp(jj+[0 1]),'Color',rand(1,3),'LineWidth',randi(5));
end
end
  2 Comments
Sim
Sim on 20 Feb 2022
Thanks a lot for your nice explanation! Very informative and useful!
Sim
Sim on 21 Feb 2022
Another solution just using plot:
x = [0 1 1 0];
y = [0 0 1 1];
hold on
patch(x,y,'red','FaceAlpha',0.2,'LineStyle','none');
plot(x(2:3),y(2:3),'r','linewidth',3)
hold off

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!