y tick labels don't match

5 views (last 30 days)
MINA
MINA on 16 Jun 2016
Edited: MINA on 17 Jun 2016
I am trying to plot two graphs on on figure, one uses right y axis and then other the left y axis. However, when I am plotting the first one on the left y axis it marks the right y axis too which would mess up the original right y ticks.
When I define the axis everything is fine but as soon as I plot my graphs, where in the code is (plot(rand(1,150),'r', 'Parent', ax1)) it causes this problem.
Can any one please help me out to resolve this issue. Here is my code:
if true
% code
end
close all
h=figure('units','normalized','outerposition',[0 0 1 1]);
whitebg(1,'k')
ax1 = gca;
get(ax1,'Position');
set(ax1,'XColor','white',...
'YColor','white',...
'YLim',[0 1],'XLim',[1 150]);
set(get(ax1, 'Ylabel'), 'String', 'R1','fontsize',10);
set(get(ax1, 'Xlabel'), 'String', 'Time (s)','fontsize',10);
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','bottom',...
'YAxisLocation','right',...
'Color','none',...
'XColor','white',...
'YColor','white',...
'YLim',[0 1.3],'XLim',[1 150],...
'XTick',[],'XTickLabel',[]);
set(get(ax2, 'Ylabel'), 'String', 'R2','fontsize',10);
hold on
p1=plot(rand(1,150),'r', 'Parent', ax1);
hold on
p2=plot(rand(1,150),'g', 'Parent', ax2);
legend([p1 p2],'R1', 'R2')
P.S. Even if I use the same range for yaxis, it works fine as long as I don't use legend. If I use legend again I have two tick marks on the right y axis:
second version of the code:
if true
% code
end
close all
h=figure('units','normalized','outerposition',[0 0 1 1]);
whitebg(1,'k')
ax1 = gca;
get(ax1,'Position');
set(ax1,'XColor','white',...
'YColor','white',...
'YLim',[0 1],'XLim',[1 150]);
set(get(ax1, 'Ylabel'), 'String', 'R1','fontsize',10);
set(get(ax1, 'Xlabel'), 'String', 'Time (s)','fontsize',10);
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','bottom',...
'YAxisLocation','right',...
'Color','none',...
'XColor','white',...
'YColor','white',...
'YLim',[0 1],'XLim',[1 150],...
'XTick',[],'XTickLabel',[]);
set(get(ax2, 'Ylabel'), 'String', 'R2','fontsize',10);
hold on
p1=plot(rand(1,150),'r', 'Parent', ax1);
hold on
p2=plot(rand(1,150),'g', 'Parent', ax2);
legend([p1 p2],'R1', 'R2')

Answers (0)

Community Treasure Hunt

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

Start Hunting!