How can I fill the area under a curve with different colors?
36 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1537220/image.png)
0 comentarios
Respuesta aceptada
Voss
el 12 de Nov. de 2023
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 comentarios
Más respuestas (2)
John D'Errico
el 12 de Nov. de 2023
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 comentarios
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!