# How to shade the same area in each subplot in a 5x1 Figure?

4 views (last 30 days)
S_G on 18 Sep 2022
Answered: Simon Chan on 18 Sep 2022
I am trying to create a 5x1 subplot figure using five different subsets of data measured by a chemical sensor but with a common x-axis. I am trying to create a grey-shaded region for a common date/time range across each subset of data in each subplot that shows an important feature that willl then be plotted in its own separate figure. I have successfullly created the grey-shaded region in sublots 1, 4, and 5 but cannot get to show up in subplots 2 and 3 regardless of what I have tried. Where I succeeded in subplots 1, 4, and 5, I simply use the area plotting function to shade the region of interest first and then superimpose everything else on top of it. This did not work for subplots 2 and 3 nor did the fliplr function work either that can help shade an area bounded by X1/X2 and Y1/Y2. Figure (so far is shown below).
A copy of the script, and input data in a .xlsx file (loaded as column vectors into MATLAB) are attached. I run MATLAB version R2022a. Any help would be appreciated. Thank you in advance.

Jeffrey Clark on 18 Sep 2022
@S_G, if you use debug to step thru the code you will see that the 2nd subplot initially gets the shading, but then line plotted is just below it. When the ylim([-0.936 -0.864]) is done the shaded area is excluded; appearently the eext1 doesn't cover the extents of eext. And in your attached code you didn't even try to shade the third subplot?
Always best to use the debugger to step thru problems.

Simon Chan on 18 Sep 2022
If you would like to shade the entire region and already knowing the y-limits, you can put the value of those limits directly which make sure it appears in the plot region.
On the other hand, you may also try to use function rectangle to shade the region.
datenum1 = datenum(rawdata.Y1,rawdata.M1,rawdata.D1,rawdata.H1,rawdata.MN1,rawdata.S1);
h1 = subplot(2,1,1);
area(datenum1,repelem(0.3,length(datenum1),1),'BaseValue',-1.02,'FaceColor',[240/256 240/256 240/256],'EdgeColor',[240/256 240/256 240/256],'LineStyle','-');
line(datenum1,rawdata.deltaphie,'Color','k','LineWidth',1.5);
zeroline = refline(0,0);
set(zeroline,'Color','k','LineWidth',0.25,'LineStyle','-');
ylim([-1.02 0.3]);
set(gca,'FontSize',9);
set(gca,'ytick',[-0.9 -0.6 -0.3 0 0.3]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.1f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('\DeltapH^{INT-EXT}','FontSize',9,'FontWeight','bold');
date_frmt = 'dd-mmm-yyyy';
beginTime = '27-Jan-2016';
endTime = '11-Feb-2016';
xlim([datenum(beginTime,date_frmt) datenum(endTime,date_frmt)]);
xticks(736356:3:736371);
set(gca,'xticklabel',[])
text(736354.7,0.55,'a','FontSize',15,'FontWeight','bold');
box on
%
h2 = subplot(2,1,2);
area(datenum1,repelem(-0.864,length(datenum1),1),'BaseValue',-0.936,'FaceColor',[240/256 240/256 240/256],'EdgeColor',[240/256 240/256 240/256],'LineStyle','-');
line(datenum1,rawdata.eext,'Color','b','LineWidth',1.5);
ylim([-0.936 -0.864]);
set(gca,'FontSize',9);
set(gca,'ycolor','b');
set(gca,'ytick',[-0.930 -0.915 -0.900 -0.885 -0.870]);
ylabel('E_{EXT} (V)','FontSize',9,'FontWeight','bold');
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.3f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
date_frmt = 'dd-mmm-yyyy';
beginTime = '27-Jan-2016';
endTime = '11-Feb-2016';
xlim([datenum(beginTime,date_frmt) datenum(endTime,date_frmt)]);
xticks(736356:3:736371);
set(gca,'xticklabel',[])
text(736354.7,-0.865,'b','FontSize',15,'FontWeight','bold');
box on

### Categories

Find more on Subplots 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!

Translated by