MATLAB Answers

Shade areas between graphs

2 ビュー (過去 30 日間)
Sreerag K
Sreerag K 2020 年 10 月 19 日
コメント済み: Sreerag K 2020 年 10 月 19 日
I need to shade the areas of graph above y = 1 line and below y = -1 line. How can I do code for that.
Like this one.

  0 件のコメント

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

採用された回答

Sudheer Bhimireddy
Sudheer Bhimireddy 2020 年 10 月 19 日
Look at area() function in MATLAB. May be that helps with your code.
Below is an example:
x = -3*pi : 0.1 :3*pi;
y_signal = sin(2*x)+sin(1.7*x); % Random signal
threshold = 0.7; % Area lower boundary
figure(1);
clf;
hold on;
area(x, max(y_signal, threshold), threshold,'EdgeColor', 'none', 'FaceColor', 'b');
plot(x, y_signal,'k-',lsz,2);
Here is the result:

  3 件のコメント

Sreerag K
Sreerag K 2020 年 10 月 19 日
It worked very well in the positve y axis. But I need to do the same for the negative y axis which was not working with this code.
To do this same in the negative y axis what should I give as the baseline argument for area.
Sudheer Bhimireddy
Sudheer Bhimireddy 2020 年 10 月 19 日
I was able to replicate the problem with two baselines. For now, may be this workaround might help.
x = -3*pi : 0.1 :3*pi;
y_signal = sin(2*x)+sin(1.7*x); % Random signal
threshold_high = 0.7;
threshold_low = -0.5;
figure(1);
clf;
hold on;
area(x, max(y_signal, threshold_high), threshold_high,'EdgeColor', 'none', 'FaceColor', 'b');
area(x, min(y_signal, threshold_low), threshold_low, 'EdgeColor', 'none', 'FaceColor', 'g');
% Now mask the unwanted region in between white
area([min(x) max(x)], [threshold_high threshold_high],threshold_low,'EdgeColor', 'none', 'FaceColor', 'w','ShowBaseLine', 'off');
plot(x, y_signal,'k-',lsz,2);
plot([min(x) max(x)], [threshold_high threshold_high],'k--',lsz,2);
plot([min(x) max(x)], [threshold_low threshold_low], 'k--',lsz,2);
Output:
Sreerag K
Sreerag K 2020 年 10 月 19 日
Thank you. It worked fine

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

その他の回答 (0 件)

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by