필터 지우기
필터 지우기

How can I fill the area under a curve with different colors?

조회 수: 54 (최근 30일)
Navid
Navid 2023년 11월 12일
편집: Matt J 2023년 11월 12일
Hi. I want to inquire about filling the area under a curve with distinct hues. Your assistance with this matter would be greatly appreciated. Thank you.

채택된 답변

Voss
Voss 2023년 11월 12일
Here's one way:
x = 1:100;
y = log(x);
xb = [1 15 55.5 100];
yb = interp1(x,y,xb);
plot(x,y)
hold on
colors = 'gyr';
for ii = 1:numel(xb)-1
idx = (x >= xb(ii)) & (x < xb(ii+1));
x_fill = [xb(ii), xb(ii), x(idx), xb(ii+1), xb(ii+1)];
y_fill = [0, yb(ii), y(idx), yb(ii+1), 0 ];
fill(x_fill,y_fill,colors(ii));
end

추가 답변 (2개)

John D'Errico
John D'Errico 2023년 11월 12일
Simple.
Just use fill (or patch) to fill THREE patches, each with the desired color. How can it be any simpler?
Or, you could build 3 polyshapes, then plot then with the desired color. Again, quite easy.

Matt J
Matt J 2023년 11월 12일
편집: Matt J 2023년 11월 12일
One way:
x=0:0.01:4;
y=1-exp(-x);
color={'g','y','r'};
edges=0:4;
for i=1:3
r=(edges(i)<=x)&(x<=edges(i+1));
area(x(r), y(r), FaceColor=color{i}); hold on
end; hold off

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by