How to plot a patch?
조회 수: 9 (최근 30일)
이전 댓글 표시
I have a plot, but I want to crop a rounded rectangle area from it. For example I have a sinusoid plane wave image plot, and I want to draw only part of it, a rounded rectangle area. Is it posible to do it easily?
댓글 수: 2
bio lim
2016년 11월 28일
Can you give the code for your rectangle plot so that I can take a look at it?
답변 (2개)
Star Strider
2016년 12월 17일
Try this (and a short tutorial on the patch funciton):
The Code —
x = linspace(0, 2*pi, 1000);
y = sin(x);
sub_area_idx = [find((y <= 0.5) & (x <= pi)) find((y >= -0.5) & ((x > pi) & (x <= 2*pi)))];
figure(1)
plot(x,y,'r')
hold on
patch(x([sub_area_idx, flip(sub_area_idx)]), [y(sub_area_idx), zeros(size(sub_area_idx))], 'g')
hold off
grid
The ‘x’ (independent variable) and ‘y’ (dependent variable) together define the entire sine curve. I arbitrarily chose ± ½ as the amplitude for the patch. The ‘sub_area_idx’ are indices into both ‘x’ and ‘y’ for those criteria.
In the patch call, the independent variable ‘x’ has to ‘retrace its steps’ (thus the flip call) to complete the curve, while the dependent variable ‘y’ only has to define those ‘retraced steps’ as zero, since (by our definition here) the patch only exists between the ‘x’-axis and ‘y’ at any particular point.
I’m not certain what you want to do, but this should at least get you part way there.
The Plot —
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176114/image.png)
댓글 수: 0
Farouk Moukaddem
2016년 12월 13일
Hi Mr M,
You can use the "imcrop" function to create an interactive image cropping tool associated with the image displayed in your current figure.
The following command can be used to crop the image "I" according to the cropping rectangle "rect"
>>imcrop(I,rect)
"rect" is a four-element position vector of the form [xmin ymin width height] that specifies the size and the position of the crop rectangle. To obtain the coordinates of the rectangle that you would like to crop, you can use the "getrect" function to obtain these values.
Refer to the following documentation links for more information:
Thanks,
Farouk
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!