This is expected behavior when adding a second axes on a subplot figure. The plot boxes can be lined up using the 'align' option in the SUBPLOT function. To do this, change
to
For example,
x1 = 1:100; y1 = x1*ones(length(x1));
x2 = 1:100; y2 = cos(pi*x2/6);
subplot(2,2,1,'align')
plot(x1,y1,'Color','r');
set(gca,'XColor','r','YColor','r');
axes('Position',get(gca,'Position'), 'XAxisLocation','top',...
'YAxisLocation','right','Color','none', 'XColor','k','YColor','k');
hold on;
plot(x2,y2,'Color','k');
subplot(2,2,2,'align')
plot(x1,y1,'Color','r');
set(gca,'XColor','r','YColor','r');
axes('Position',get(gca,'Position'), 'XAxisLocation','top',...
'YAxisLocation','right','Color','none', 'XColor','k','YColor','k');
hold on;
plot(x2,y2,'Color','k');
subplot(2,2,3,'align')
plot(x1,y1,'Color','r');
set(gca,'XColor','r','YColor','r');
axes('Position',get(gca,'Position'), 'XAxisLocation','top',...
'YAxisLocation','right','Color','none', 'XColor','k','YColor','k');
hold on;
plot(x2,y2,'Color','k');
subplot(2,2,4,'align')
plot(x1,y1,'Color','r');
set(gca,'XColor','r','YColor','r');
axes('Position',get(gca,'Position'), 'XAxisLocation','top',...
'YAxisLocation','right','Color','none', 'XColor','k','YColor','k');
hold on;
plot(x2,y2,'Color','k');
Best Answer