Shade Area Between 2 Tabulated Plots.

1 回表示 (過去 30 日間)
Rob W
Rob W 2019 年 10 月 12 日
コメント済み: Star Strider 2019 年 10 月 13 日
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!

採用された回答

Star Strider
Star Strider 2019 年 10 月 12 日
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 件のコメント
Rob W
Rob W 2019 年 10 月 13 日
Wow! I really appreicate that, Thanks a bunch mate.
Star Strider
Star Strider 2019 年 10 月 13 日
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.)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by