How can I shade the background of a timeseries plot based on another value.

17 ビュー (過去 30 日間)
Sara Warix
Sara Warix 2020 年 7 月 17 日
コメント済み: Sara Warix 2020 年 7 月 17 日
I have the following code:
days = [152:272]; %n = 120
PA = [0 0 0 0 1 1 1 1....1 0]; % 1 or 0 for each of the 120 day time series
I would like to shade the back of the plot blue when PA = 1 and yellow when PA = 0
I am going to plot another line in front of the color patches.

採用された回答

jonas
jonas 2020 年 7 月 17 日
編集済み: jonas 2020 年 7 月 17 日
You could do something like this,
%some data
days = 100:199;
PA = repmat([0 0 0 0 1 1 1 1 1 1],1,10);
%PA = 1 -> blue background
[x,y] = stairs(days,PA);
area(x,y,'edgecolor','none','facecolor','b')
%otherwise yellow background
set(gca,'color','y')
  6 件のコメント
jonas
jonas 2020 年 7 月 17 日
編集済み: jonas 2020 年 7 月 17 日
This should do it. You just need to experiment with the basevalue-property of the stairs-objects and the y-values derived from PA.
%some data
days = 100:199;
PA = repmat([0 0 0 0 1 1 1 1 1 1],1,10);
%get cornerpoints for blue areas
[x,y] = stairs(days,PA);
%let blue background go from -2000 to 2000
y = y.*4e3 - 2e3;
a = area(x,y,'edgecolor','none','facecolor','b')
%adjust basevalue
a.BaseValue = min(y);
%set axes background to yellow
set(gca,'color','y')
hold on
%plot some data
plot([100,200],[-2,2000],'k')
%change axis to your desired limits
axis([min(days),max(days),-2,2000])
Sara Warix
Sara Warix 2020 年 7 月 17 日
great, thank you

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

その他の回答 (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