![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307403/image.png)
Fill area between plot and the 0-line?
116 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Julozert
el 31 de Mayo de 2020
Comentada: Harshit
el 31 de Oct. de 2022
Hi,
I want to plot some data to show a difference to a reference value and i want that the curve above zero gets filled red and the area of the plot below zero gets filled with blue.
How do i do that?
0 comentarios
Respuesta aceptada
Akira Agata
el 1 de Jun. de 2020
The following is an example:
% Sample data
x = linspace(0,4*pi,1000);
y = sin(x);
% Extract positive and negative part
yp = (y + abs(y))/2;
yn = (y - abs(y))/2;
% Plot the data using area function
figure
area(x,yp,'FaceColor','r')
hold on
area(x,yn,'FaceColor','b')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307403/image.png)
2 comentarios
Más respuestas (2)
the cyclist
el 31 de Mayo de 2020
3 comentarios
the cyclist
el 31 de Mayo de 2020
The way I would do it is to use your data points to define polygons that are above zero, and polygons that are below zero, and then use patch or fill, to fill them in.
Image Analyst
el 1 de Jun. de 2020
For a more general situation, see the FAQ: https://matlab.fandom.com/wiki/FAQ#How_do_I_shade_the_region_between_two_curves.3F
0 comentarios
Ver también
Categorías
Más información sobre Polygons 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!