MATLAB Answers

Fill area between plot and the 0-line?

338 views (last 30 days)
Julozert
Julozert on 31 May 2020
Answered: Image Analyst on 1 Jun 2020
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?

Accepted Answer

Akira Agata
Akira Agata on 1 Jun 2020
How about using area function?
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')

More Answers (2)

the cyclist
the cyclist on 31 May 2020
You can use either the fill or patch command. See the examples there for guidance.
  3 Comments
Julozert
Julozert on 31 May 2020
How can i use my data points to define polygons?

Sign in to comment.


Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!