How can I fill in the area between two curves? The curves are not the same length and also don't form a closed shape.

29 views (last 30 days)
The plot at the top is an example of what I have. There are two data series. The top series (filled squares) represents maximum values of my data. For each maximum value data point, there is usually (but not always) a minimum value data point vertically below.
I would like to do something like in the second figure, where I shade the area between the two curves--or better still, between each data pair (the maximum and minimum value at a particular x axis value) and plot one of the points in the pair as solid.
My problem is that I have been through previous questions here and can't get anything to work for my situation. In my example, there is a minimum value for each maximum value but that is not always the case and also, the two curves don't form a closed shape. The errors I'm getting seem to be related to these issues. Does anyone know if there's a straightforward way to do this? Thank you for any advice.
Here are the data. I left the "NaN" in because that's the nature of my data and I have to deal with them.
%x1 and x2
1.0000
3.0000
4.5000
6.0000
8.0000
11.0000
16.0000
20.0000
25.0000
30.0000
16.0027
24.1943
27.7769
34.8065
44.5189
56.2825
68.2554
73.0484
76.4253
NaN
%y2
14.0410
20.9402
25.0441
30.4932
36.3988
50.6604
61.9821
71.4331
73.9582
NaN

Accepted Answer

Voss
Voss on 8 Apr 2022
x1 = [1.0000; 3.0000; 4.5000; 6.0000; 8.0000; 11.0000; 16.0000; 20.0000; 25.0000; 30.0000];
x2 = [16.0027; 24.1943; 27.7769; 34.8065; NaN; 56.2825; 68.2554; 73.0484; 76.4253; NaN];
y2 = [14.0410; NaN; 25.0441; 30.4932; 36.3988; 50.6604; 61.9821; 71.4331; 73.9582; NaN];
% 44.5189 replaced by NaN in x2 for demonstration
% 20.9402 replaced by NaN in y2 for demonstration
good_x2 = find(~isnan(x2));
good_y2 = find(~isnan(y2));
patch( ...
x1([good_x2; good_y2(end:-1:1)]), ...
[x2(good_x2); y2(good_y2(end:-1:1))], ...
'r','EdgeColor','none','FaceAlpha',0.5)
hold on
plot(x1,x2,'rs','MarkerFaceColor','r')
plot(x1,y2,'rs')
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!