I have a 6-by-3 set of subplots where two subplots are always related. I want to change the background color behind two of these plots (not the background of plots themselves) to "connect" them optically.
I tried it with a rectangle annotation but there is no way to get it behind the plots. uistack didn't work either. Using the 'bottom' option, the rectangle is still in front of the plots.
Is there any way to draw on the background behind plots?
Here's a little example of how you might do this with axes to create an axis with a colored background and uistack to move it to the back:
figure
h1 = subplot(2,2,1);
h2 = subplot(2,2,2);
h3 = subplot(2,2,3);
h4 = subplot(2,2,4);
p1 = get(h1,'Position');
p2 = get(h2,'Position');
border = 0.3*p1(1);
x1 = p1(1)-border;
y1 = p1(2)-border;
width1 = p2(3)+p2(1)-p1(1)+2*border;
height1 = max(p1(4),p2(4))+2*border;
ax1 = axes('Position', [x y width1 height1],...
'Color','r','XTick',[],'XColor','r','YTick',[],'YColor','r');
uistack(ax1,'bottom')
p3 = get(h3,'Position');
p4 = get(h4,'Position');
border = 0.3*p3(1);
x2 = p3(1)-border;
y2 = p3(2)-border;
width2 = p4(3)+p4(1)-p3(1)+2*border;
height2 = max(p3(4),p4(4))+2*border;
ax2 = axes('Position', [x2 y2 width2 height2],...
'Color','b','XTick',[],'XColor','b','YTick',[],'YColor','b');
uistack(ax2,'bottom')
This produces a figure that looks like this one :

You can simply set the Color property of each axes object to the color you like. For example:
subplot(2,2,1)
plot(1:3,1:3) % example plot
set(gca,'Color',[.5 .5 .8]) % set background color
subplot(2,2,2)
plot(1:3,1:3)
set(gca,'Color',[.5 .5 .8]) % set background color
subplot(2,2,3)
plot(1:3,1:3)
subplot(2,2,4)
plot(1:3,1:3)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With