How to add gray bands in a plot

13 ビュー (過去 30 日間)
Tiziana Lazzarina Zendrini
Tiziana Lazzarina Zendrini 2023 年 2 月 7 日
I have a figure with multiple subplot with the same time series, I would like to add a gray band on the x axis every year, do you have any idea?
  2 件のコメント
Jan
Jan 2023 年 2 月 7 日
Can you draw the "gray band" manually, such that it gets clear what "every year" exactly means? Maybe you want to set 'XGrid' to 'on' in each plot?
Tiziana Lazzarina Zendrini
Tiziana Lazzarina Zendrini 2023 年 2 月 8 日
Yes, sorry for the unclear explanation, I would like to do something like this for each subplot and not just to set the XGrid on:
thank you very much for your reply.

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

回答 (1 件)

DGM
DGM 2023 年 2 月 7 日
編集済み: DGM 2023 年 2 月 7 日
Here's my interpretation. I'm assuming you're using datetime for the xdata, so
% some fake data
t1 = datetime(2013,11,1,8,0,0);
t2 = datetime(2020,11,5,8,0,0);
t = t1:caldays(5):t2;
y = rand(1,numel(t))+10;
% specify desired yaxis range
yaxrange = [9 12];
% plot the area under a square wave
yrbk = mod(year(t),2); % every other year
yrbk = abs(diff(yaxrange))*yrbk+min(yaxrange); % rescale on y
area(t,yrbk,min(yaxrange),'facecolor',0.9*[1 1 1],'edgecolor','none')
% plot the data on top of it
hold on
plot(t,y)
ylim(yaxrange)
  1 件のコメント
Tiziana Lazzarina Zendrini
Tiziana Lazzarina Zendrini 2023 年 2 月 8 日
Thanks a lot!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by