Why are the rightmost axes from two or more subplots not aligned when creating the subplots with y-axes on the right and left hand side?

7 views (last 30 days)
When creating subplots with Y-axes on both left and right side (without using the PLOTYY function), the right hand side axes do not align. For example:
x1 = 1:100; y1 = x1*ones(length(x1));
x2 = 1:100; y2 = cos(pi*x2/6);
subplot(2,2,1)
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)
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)
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)
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');
If you enlarge the figure you will note that the axes on the right hand side of each subplot. They should lie exactly one over another; in this case, they do not align.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Oct 2009
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
subplot(2,2,1)
to
subplot(2,2,1,'align')
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');

More Answers (0)

Categories

Find more on Two y-axis in Help Center and File Exchange

Products


Release

R14SP1

Community Treasure Hunt

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

Start Hunting!