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일

0 개 추천

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일
Hello @Adam, it's very interesting, Thank you for your response, while I'm in doubt that where did you use that Array "a" (mentioned in question) in the code, becasue incase if I have to use same code for another plot where I have to put that? Secondly, why can't we use any other filled pattern in this case. Thank once again.
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
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
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개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

태그

질문:

2019년 12월 10일

편집:

2019년 12월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by