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.

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

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

Thank you very much! This is great. If you have time, it would be great to know how to make the edges a little smoother/blobbier so they look beautiful, but please don't feel obliged. Really grateful.
To make the edges smoother, you'd have to introduce additional data points, e.g., by interpolating between the given data using some non-linear method (because using linear interpolation will give you the same straight-line segments you already have)
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
% only interpolate with non-NaN values:
good_x2 = find(~isnan(x2));
good_y2 = find(~isnan(y2));
% interpolate to 100 points
x1_patch = linspace(min(x1),max(x1),100).';
x2_patch = interp1(x1(good_x2),x2(good_x2),x1_patch,'makima',NaN);
y2_patch = interp1(x1(good_y2),y2(good_y2),x1_patch,'makima',NaN);
% make the patch the same way as before, but now use the interpolated
% points:
good_x2 = find(~isnan(x2_patch));
good_y2 = find(~isnan(y2_patch));
patch( ...
x1_patch([good_x2; good_y2(end:-1:1)]), ...
[x2_patch(good_x2); y2_patch(good_y2(end:-1:1))], ...
'r','EdgeColor','none','FaceAlpha',0.5)
hold on
plot(x1,x2,'rs','MarkerFaceColor','r')
plot(x1,y2,'rs')
Thank you, that's very kind. Really appreciate your help.

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

Asked:

on 8 Apr 2022

Commented:

on 8 Apr 2022

Community Treasure Hunt

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

Start Hunting!