필터 지우기
필터 지우기

Hatchfill certain area under curve: findobj

조회 수: 17 (최근 30일)
peterhack
peterhack 2016년 6월 19일
댓글: Walter Roberson 2016년 6월 19일
Hi,
I was wondering how to get the area left to the line where the mean is hatched in color using hatchfill2 function:
x = [-3:.1:3];
norm = normpdf(x,0,1);
figure;
plot(x,norm)
y1 = get(gca,'ylim');
hold on
plot([mean(norm) mean(norm)],y1)
I am aware this tutorial, however, I already struggle getting the right area with findobj.
Thanks in advance!

채택된 답변

Walter Roberson
Walter Roberson 2016년 6월 19일
You plotted the mean as a line instead of as a patch. The hatchfill and hatchfill2 File Exchange contributions require patch objects.
nmean = mean(norm);
on_left_side = x <= nmean;
left_x = x(on_left_side);
left_y = norm(on_left_side);
patch_x = [left_x(:); left_x(end); left_x(1)];
patch_y = [left_y(:); left_y(1); left_y(1)];
h = patch(patch_x, patch_y, 'w');
hatchfill(h);
If you look carefully at the result before doing the hatch fill you will note that the patch created might end slightly to the left of the line. This has to do with the fact that your x is quantized by your colon operator so the mean can fall between two x values.
I considered posting code to do the interpolation so you got a good edge match, but I did not bother working it out, because your line is fundamentally meaningless. norm is your y coordinate and you are taking the mean over that y coordinate, and you are then turning that mean y coordinate into an x coordinate. It would make a lot more sense if you were drawing a horizontal line and hatching above or below it.

추가 답변 (1개)

peterhack
peterhack 2016년 6월 19일
Many thank for your reply!
I am aware of the values that they are meaningless. This was just an example to get going.
Anyway, one more question. I would like to color the hatch similiar to the turial, but I do get this: "There is no Color property on the Patch class." Do I need to use another object class?
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 6월 19일
patch objects have CData and FaceVertexCData properties. The two affect coloring in different ways, with the vertex based one being more powerful in some ways.
The output of hatchfill is line objects, which do have a Color property.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surfaces, Volumes, and Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by