How can I fill the area under a curve with different colors?
64 次查看(过去 30 天)
显示 更早的评论
采纳的回答
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
0 个评论
更多回答(2 个)
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!