Color the area between two plots

I'm trying to color the area between two plots with the "fill" function. I was actually able to do it, but for some reasons the area is just filled with some vertical black lines instead of being colored as it should. How can I fix this?

4 Comments

Sam Chak
Sam Chak on 11 Sep 2023
Edited: Sam Chak on 11 Sep 2023
@Ale_798, Please edit the question to include the MATLAB code with 'fill()'.
Click on this icon and insert the code.
Ale_798
Ale_798 on 11 Sep 2023
Edited: Ale_798 on 11 Sep 2023
x1 = [x' ; flipud(x')];
area = [plot1 ; flipud(plot2)];
fill(x1, area, 'b');
@Ale_798, Can you address this error message so that we can duplicate the plot in your image?
x1 = [x' ; flipud(x')];
Unrecognized function or variable 'x'.
area = [plot1 ; flipud(plot2)];
fill(x1, area, 'b');
Looks like your areas are between points sequentially between the two curves, not along the horizontal axes...
We need the full code to regenerate the figure in order to tell...pictures and incomplete code with undefined variables aren't enough to go on without a lot of extra work..."Help us help you!"

Sign in to comment.

Answers (1)

One way to achive this is like this:
x1 = 1:0.05:2; % graph 1
y1 = exp(x1) + 1;
x2 = 1:0.05:2; % graph 2
y2 = exp(x2);
figure(1); hold on
fill([x1, flip(x2)], [y1, flip(y2)],'y') % plot filled area
plot(x1,y1,x1,y2, "LineWidth",3) % plot both graphs
Note that the second graph is flipped while plotting the area to continue the polygon in the other direction. For more Info please check the fill() function documentation.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Asked:

on 11 Sep 2023

Answered:

on 11 Sep 2023

Community Treasure Hunt

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

Start Hunting!