Shading an area boundered by a curve

后端 未结 3 2170
小鲜肉
小鲜肉 2020-12-09 22:42

What would be the easiest way to lightly shade (or hatch; or anything to set it different from the rest) an area in a plot(), below a curve y=x^2, for example ?



        
3条回答
  •  难免孤独
    2020-12-09 23:25

    A supplemental example to elaborate on Doresoom's post:

    x=0:pi/50:2*pi;
    y1=x.^2;
    y2=10+5*sin(3*x);
    baseval1=20;
    baseval2=3;
    clf;
    hold on;
    H1=area(x,y1,baseval1);
    H2=area(x,y2,baseval2);
    hold off;
    h=get(H1,'children');
    set(h,'FaceAlpha',0.5,'FaceColor',[1 0.5 0]);
      % set color to orange, alpha to 0.5
    h=get(H2,'children');
    set(h,'FaceAlpha',0.5,'FaceColor',[0.85 1 0.25]);
      % set color to yellow-green, alpha to 0.5
    

    But where do you set the color ?

    h is a handle to a patch (a filled in area); if you type get(h) you can see all its properties. The MATLAB docs on patch properties explain these to some degree.

    And how for example would you shade an area above the curve with that principle ?

    area creates a patch between a base value and a curve. Doesn't look like there's an easy way to create an area between two curves though.

提交回复
热议问题