# Filling area with pattern

20 views (last 30 days)
shane watson on 10 Dec 2019
Edited: Adam Danz on 11 Dec 2019
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 on 10 Dec 2019
Edited: Adam Danz on 11 Dec 2019
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.
% 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);
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.

Show 1 older comment
Adam Danz on 10 Dec 2019
The first 4 lines of my answer are from the last 2 lines of your code. I just took out the first 2 inputs to fill() and made them into variables. You could add the rest of your code on top of it.
"why can't we use any other filled pattern in this case"
There are no such patterns in Matlab. This really isn't a fill. It's just an array of equally spaced dots. You could change things up by replacing the last line in my answer with any of the following examples
h = plot(inX,inY,'k.','MarkerSize',4,'MarkerFaceColor','k');
h = plot(inX,inY,'ks','MarkerSize',4,'MarkerFaceColor','k');
h = plot(inX,inY,'ks','MarkerSize',4,'MarkerFaceColor','r');
h = plot(inX,inY,'k^','MarkerSize',4,'MarkerFaceColor','k');
h = plot(inX,inY,'kh','MarkerSize',4,'MarkerFaceColor','y');
% etc.....
shane watson on 10 Dec 2019
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 :)
Adam Danz on 11 Dec 2019
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
%.....................................^