align problem in multiple axes

12 views (last 30 days)
Chong Hou
Chong Hou on 17 Apr 2011
I want to plot two lines in one graph (in one subplot, if not in the subplot, there is no problem) and one axes on bottom and left, the other axes on top and right. However, when I add xlabel to one axes system, the two y axis don't match at the bottom line. the code is as follow:
subplot(4,1,4);
line(time,std);
ax1=gca;
set(ax1,'color','none','Xcolor',xcolor,'ycolor',leftcolor,...
'xminortick','on','yminortick','on','xlim',[t(1),t(2)],...
'ylim',[0.01,100],'yscale','log');
set(get(ax1,'ylabel'),'string','Std (%)','parent',ax1);
set(get(ax1,'xlabel'),'string','time (s)','parent',ax1);
ax2=axes('position',get(ax1,'position'),'xaxislocation','top',...
'yaxislocation','right','color','none',...
'xticklabel','','xminortick','on','yminortick','on',...
'ylim',[0.01,100],'yscale','log');
%set(get(ax2,'xlabel'),'string','time (s)','parent',ax2);
Could anyone tell me how to solve this problem? Thanks!

Answers (2)

Oleg Komarov
Oleg Komarov on 17 Apr 2011
Have you tried plotyy?
EDIT
What problems you have with plotyy?
With your example:
time = [0.974 1.232 1.342 1.315 1.539 1.654 1.618 1.275 0.764 0.332 0.294 0.451 0.493 0.405 0.278 0.447 0.800 0.965 0.873 0.601 0.340 0.489 0.634 0.661 0.582 0.591 0.659 0.643 0.508 0.332 0.558 0.823 0.836 4.95 4.693 4.522 4.441 4.372 0.526 0.531 0.777 0.951 0.911 0.823 0.498 0.298 0.260 0.306 0.437 0.617 0.646 0.614 0.448 0.285 0.163 0.0950 0.149 0.248 0.338 0.365 0.422 0.435 0.411 0.377 0.241 0.185 0.371 0.529 0.513 0.346 0.271 0.320 0.283 0.338 0.647 0.953 1.099 0.959 0.620 0.333 0.347 0.615 0.709 0.550 0.724 2.011 3.016 2.659 1.991 1.762 1.914 1.202 1.123 0.976 0.863 0.740 0.600 0.488 0.438 0.465 0.563 0.666 0.676 0.597 0.486 0.346 0.288 0.255 0.226 0.261 0.353 0.429 0.454 0.353 0.216 0.191 0.336 0.474 0.567 0.565];
std = [0.974 1.232 1.342 1.315 1.539 1.654 1.618 1.275 0.764 0.332 0.294 0.451 0.493 0.405 0.278 0.447 0.800 0.965 0.873 0.601 0.340 0.489 0.634 0.661 0.582 0.591 0.659 0.643 0.508 0.332 0.558 0.823 0.836 4.95 4.693 4.522 4.441 4.372 0.526 0.531 0.777 0.951 0.911 0.823 0.498 0.298 0.260 0.306 0.437 0.617 0.646 0.614 0.448 0.285 0.163 0.0950 0.149 0.248 0.338 0.365 0.422 0.435 0.411 0.377 0.241 0.185 0.371 0.529 0.513 0.346 0.271 0.320 0.283 0.338 0.647 0.953 1.099 0.959 0.620 0.333 0.347 0.615 0.709 0.550 0.724 2.011 3.016 2.659 1.991 1.762 1.914 1.202 1.123 0.976 0.863 0.740 0.600 0.488 0.438 0.465 0.563 0.666 0.676 0.597 0.486 0.346 0.288 0.255 0.226 0.261 0.353 0.429 0.454 0.353 0.216 0.191 0.336 0.474 0.567 0.565];
[ax, h1, h2] = plotyy(time, std,time,std);
set(ax,'xminortick','on','yminortick','on',...
'ylim',[0.01,100],'yscale','log','ygrid','on','ycolor','k');
set([h1 h2],'Color','b')
set(cell2mat(get(ax,'ylabel')),'string','Std (%)');
set(get(ax(1),'xlabel'),'string','time (s)');
  1 Comment
Chong Hou
Chong Hou on 17 Apr 2011
Yes I did try plotyy but there is other problem associated with plotyy.

Sign in to comment.


Matt Fig
Matt Fig on 17 Apr 2011
When I run your code, with some made-up values for time, std, xcolor, leftcolor and t, I don't see any problem.
How about you provide a complete example, one where people don't have to make up values, and put a pause statement in so we can see where the y-axis mismatch happens? Of course your values should be enough to reproduce what you are seeing, but not so long as to make somebody have to scroll for pages...
time = ...
std = ...
xcolor = ...
leftcolor = ...
t = ...
% Plotting code, from first post
pause(2) % Here comes the mismatching
% set xlabel so that the y-axis doesn't match...
  1 Comment
Chong Hou
Chong Hou on 18 Apr 2011
Please try this, thanks!
time=[0.974000000000000 1.23200000000000 1.34200000000000 1.31500000000000 1.53900000000000 1.65400000000000 1.61800000000000 1.27500000000000 0.764000000000000 0.332000000000000 0.294000000000000 0.451000000000000 0.493000000000000 0.405000000000000 0.278000000000000 0.447000000000000 0.800000000000000 0.965000000000000 0.873000000000000 0.601000000000000 0.340000000000000 0.489000000000000 0.634000000000000 0.661000000000000 0.582000000000000 0.591000000000000 0.659000000000000 0.643000000000000 0.508000000000000 0.332000000000000 0.558000000000000 0.823000000000000 0.836000000000000 4.95000000000000 4.69300000000000 4.52200000000000 4.44100000000000 4.37200000000000 0.526000000000000 0.531000000000000 0.777000000000000 0.951000000000000 0.911000000000000 0.823000000000000 0.498000000000000 0.298000000000000 0.260000000000000 0.306000000000000 0.437000000000000 0.617000000000000 0.646000000000000 0.614000000000000 0.448000000000000 0.285000000000000 0.163000000000000 0.0950000000000000 0.149000000000000 0.248000000000000 0.338000000000000 0.365000000000000 0.422000000000000 0.435000000000000 0.411000000000000 0.377000000000000 0.241000000000000 0.185000000000000 0.371000000000000 0.529000000000000 0.513000000000000 0.346000000000000 0.271000000000000 0.320000000000000 0.283000000000000 0.338000000000000 0.647000000000000 0.953000000000000 1.09900000000000 0.959000000000000 0.620000000000000 0.333000000000000 0.347000000000000 0.615000000000000 0.709000000000000 0.550000000000000 0.724000000000000 2.01100000000000 3.01600000000000 2.65900000000000 1.99100000000000 1.76200000000000 1.91400000000000 1.20200000000000 1.12300000000000 0.976000000000000 0.863000000000000 0.740000000000000 0.600000000000000 0.488000000000000 0.438000000000000 0.465000000000000 0.563000000000000 0.666000000000000 0.676000000000000 0.597000000000000 0.486000000000000 0.346000000000000 0.288000000000000 0.255000000000000 0.226000000000000 0.261000000000000 0.353000000000000 0.429000000000000 0.454000000000000 0.353000000000000 0.216000000000000 0.191000000000000 0.336000000000000 0.474000000000000 0.567000000000000 0.565000000000000];
std=[0.974000000000000 1.23200000000000 1.34200000000000 1.31500000000000 1.53900000000000 1.65400000000000 1.61800000000000 1.27500000000000 0.764000000000000 0.332000000000000 0.294000000000000 0.451000000000000 0.493000000000000 0.405000000000000 0.278000000000000 0.447000000000000 0.800000000000000 0.965000000000000 0.873000000000000 0.601000000000000 0.340000000000000 0.489000000000000 0.634000000000000 0.661000000000000 0.582000000000000 0.591000000000000 0.659000000000000 0.643000000000000 0.508000000000000 0.332000000000000 0.558000000000000 0.823000000000000 0.836000000000000 4.95000000000000 4.69300000000000 4.52200000000000 4.44100000000000 4.37200000000000 0.526000000000000 0.531000000000000 0.777000000000000 0.951000000000000 0.911000000000000 0.823000000000000 0.498000000000000 0.298000000000000 0.260000000000000 0.306000000000000 0.437000000000000 0.617000000000000 0.646000000000000 0.614000000000000 0.448000000000000 0.285000000000000 0.163000000000000 0.0950000000000000 0.149000000000000 0.248000000000000 0.338000000000000 0.365000000000000 0.422000000000000 0.435000000000000 0.411000000000000 0.377000000000000 0.241000000000000 0.185000000000000 0.371000000000000 0.529000000000000 0.513000000000000 0.346000000000000 0.271000000000000 0.320000000000000 0.283000000000000 0.338000000000000 0.647000000000000 0.953000000000000 1.09900000000000 0.959000000000000 0.620000000000000 0.333000000000000 0.347000000000000 0.615000000000000 0.709000000000000 0.550000000000000 0.724000000000000 2.01100000000000 3.01600000000000 2.65900000000000 1.99100000000000 1.76200000000000 1.91400000000000 1.20200000000000 1.12300000000000 0.976000000000000 0.863000000000000 0.740000000000000 0.600000000000000 0.488000000000000 0.438000000000000 0.465000000000000 0.563000000000000 0.666000000000000 0.676000000000000 0.597000000000000 0.486000000000000 0.346000000000000 0.288000000000000 0.255000000000000 0.226000000000000 0.261000000000000 0.353000000000000 0.429000000000000 0.454000000000000 0.353000000000000 0.216000000000000 0.191000000000000 0.336000000000000 0.474000000000000 0.567000000000000 0.565000000000000];
subplot(4,1,4);
line(time,std);
ax1=gca;
set(ax1,'color','none',...
'xminortick','on','yminortick','on',...
'ylim',[0.01,100],'yscale','log','ygrid','on');
set(get(ax1,'ylabel'),'string','Std (%)','parent',ax1);
set(get(ax1,'xlabel'),'string','time (s)','parent',ax1);
ax2=axes('position',get(ax1,'position'),'xaxislocation','top',...
'yaxislocation','right','color','none',...
'xticklabel','','xminortick','on','yminortick','on',...
'ylim',[0.01,100],'yscale','log');
set(get(ax2,'xlabel'),'string','time (s)','parent',ax2);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by