Shade Area Between 2 Tabulated Plots.

1 view (last 30 days)
Hello,
I have written a script which plots 2 curves from excel data. Could someone please help me as to how I can shade the area between the 2 curves?
This is my Script:
UserDir = 'C:\Users\****\Downloads\Test.xlsx';
SheetNameA = 'Sheet1';
Y_Values = readtable(UserDir,'Sheet',SheetNameA,'Range','A2:A150');
X_Values1 = readtable(UserDir,'Sheet',SheetNameA,'Range','C2:C150');
X_Values2 = readtable(UserDir,'Sheet',SheetNameA,'Range','D2:D150');
Y_data = Y_Values{:,1};
X_Data1 = X_Values1{:,1};
X_Data2 = X_Values2{:,1};
plot(X_Data1,Y_data,'b','Linewidth',1);
hold on
plot(X_Data2,Y_data,'b','Linewidth',1);
hold off
Please see the attached excel sheet (and change the UserDir).
To Clarify I would like something like this:
Thanks Everyone!

Accepted Answer

Star Strider
Star Strider on 12 Oct 2019
This is as close as I can get:
Y_Values = readtable(UserDir,'Sheet',SheetNameA,'Range','A2:A150','ReadVariableNames',0);
X_Values1 = readtable(UserDir,'Sheet',SheetNameA,'Range','C2:C150','ReadVariableNames',0);
X_Values2 = readtable(UserDir,'Sheet',SheetNameA,'Range','D2:D150','ReadVariableNames',0);
Y_data = Y_Values{:,1};
X_Data1 = X_Values1{:,1};
X_Data2 = X_Values2{:,1};
Y_data = Y_data(~isnan(Y_data));
X_Data1 = X_Data1(~isnan(X_Data1));
X_Data2 = X_Data2(~isnan(X_Data2));
figure
plot(X_Data1(1:end-1),Y_data(1:end-1),'b','Linewidth',1);
hold on
plot(X_Data2(1:end-1),Y_data(1:end-1),'b','Linewidth',1);
patch([X_Data1(1:end-2); X_Data2(end-1); flipud([X_Data2(1:end-2); X_Data1(end-1)])], [Y_data(1:end-1); flipud(Y_data(1:end-1))], 'r')
hold off
xlim([750 960])
There were a number of NaN values in your vectors that were interfering with the subscript referencing. I had to eliminate them, however their elimination do not appear to have affected the result. There were also some straight lines at the tops of each curve that I had to eliminate (they would have made the patch call much more difficult), thus the ‘(1:end-2)’ indexing. The concatenation of the vectors is necessary to join the curves at both ends, to create a filled figure.
This code then produces:
Shade Area Between 2 Tabulated Plots - 2019 10 12.png
I could not isolate the problem with the area at the top of the filled area.
  5 Comments
Rob W
Rob W on 13 Oct 2019
Wow! I really appreicate that, Thanks a bunch mate.
Star Strider
Star Strider on 13 Oct 2019
As always, my pleasure!
(It turns out that I was over-thinking that. I didn’t need to add the extra element to close the curve.)

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!