Hatchfill certain area under curve: findobj
17 views (last 30 days)
Show older comments
peterhack
on 19 Jun 2016
Commented: Walter Roberson
on 19 Jun 2016
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)
Thanks in advance!
0 Comments
Accepted Answer
Walter Roberson
on 19 Jun 2016
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.
0 Comments
More Answers (1)
peterhack
on 19 Jun 2016
1 Comment
Walter Roberson
on 19 Jun 2016
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.
See Also
Categories
Find more on Surfaces, Volumes, and Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!