Filling area with pattern

63 次查看(过去 30 天)
Hello all,
I would like to know the how to fill the area with some pattern [CrossedLines, DiagonalLines, FDiagonalLines, HorizontalLines, Solid, VerticalLines, or XCrossedLines]. I already use this code to fill the area with color:
a=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
sh2=stairs(1:24,a, '*','color',[0 1 0],'MarkerEdgeColor','r', 'MarkerSize',4,'LineWidth',1.3);
x = [sh2.XData(1),repelem(sh2.XData(2:end),2)]; % Although it works fine for me but I don't what is [XData(2:end),2] means, it would be good if you explain a bit.
y = [repelem(sh2.YData(1:end-1),2),sh2.YData(end)];
viu=fill([x,fliplr(x)],[y,bottom*ones(size(y))],[0 1 0])
viu.FaceAlpha = 0.2;

采纳的回答

Adam Danz
Adam Danz 2019-12-10
编辑:Adam Danz 2019-12-11
Hi shane, filling with a pattern isn't trivial so I recommend the functions listed in the file exchange. However, here's a fairly easy way to add a polka dot pattern to the area being filled.
The coordinates used to fill form a polygon. This makes an array of random dots that span the size of your polygon and then it removes any dots that are outside of the polygon.
See inline comments for details.
% Here are the coordinates of your polygon used in fill()
polyX = [x,fliplr(x)];
polyY = [y,bottom*ones(size(y))];
viu=fill(polyX,polyY,[0 1 0])
viu.FaceAlpha = 0.2;
% Create array of polkadot coordinates that span the size of the polygon
[dotsX,dotsY] = ndgrid(...
min(polyX): 1 : max(polyX), ...
min(polyY): 1 : max(polyY));
% Determine which coordinates are on or in your polygon
[in,on] = inpolygon(dotsX,dotsY,polyX,polyY);
% extract coordinates in or on polygon
inX = dotsX(in|on);
inY = dotsY(in|on);
% Plot polkadots
hold on
h = plot(inX,inY,'k.','MarkerSize',4,'MarkerFaceColor','k');
See this answer to learn how to avoid the polkadots appearing in the legend.
  4 个评论
shane watson
shane watson 2019-12-10
That's great, lastly, just one thing to share with you. i.e, I use this for another plot belw, but as you can see that polkadots are not appearing between 0 and 1, how to fix that ?and how to increase number of polkadots. I'm sure this the last :) ooo.jpg
Adam Danz
Adam Danz 2019-12-11
In this line from my code below, the middle value determines the interval between dots along the x axis and y axis
[dotsX,dotsY] = ndgrid(...
min(polyX): 1 : max(polyX), ... % for x axis
min(polyY): 1 : max(polyY)); % for y axis
%...............^
You could adjust that number for each axis.
Alternatively you could try something like this below where the last input to linspace() determines the number of dots along the x and y axes.
[dotsX,dotsY] = ndgrid(...
linspace(min(polyX), max(polyX), 10), ... % for x axis
linspace(min(polyY), max(polyY), 10); % for y axis
%.....................................^

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by