How do I make both plots visible using plotyy?
Show older comments
Hello,
I am using the following code to generate a double plot:
[ax,h1,h2]=plotyy(X1,Y1,X2,P2);
xlabel('x','Fontname','Arial')
ylabel(ax(1),'y1')
ylabel(ax(2),'y2')
set(h1,'Color','b')
set(h2,'Color','r')
set(ax(1),'XLim',[X1(1) X1(end)])
set(ax(2),'XLim',[X1(1) X1(end)])
set(ax(1),'YLim',[0 1.05*max(Y1)])
set(ax(2),'YLim',[0 1.05*max(Y2)])
I cannot see the first plot (X1,Y1) in the resulting plotyy as well as its y-axis. How can I solve the problem? Thanks.
Answers (1)
Joseph Cheng
on 24 Jun 2014
can you give a range of what X1, Y1, X2, P2 (should it be Y2? as you call out max of Y2 in the last line). Possible items could include plotted lines are on top of each other, range of X1 and X2 do not over lap, Y1 isn't in the range of 0 to max(Y1). If i go with the example below i see both graphs. which leads me to think something maybe up with your axis limits.
X1 = 1:10;
X2 = 11:21;
Y1 = 1:10;
P2 = 11:21;
[ax,h1,h2]=plotyy(X1,Y1,X2,P2);
xlabel('x','Fontname','Arial')
ylabel(ax(1),'y1')
ylabel(ax(2),'y2')
set(h1,'Color','b')
set(h2,'Color','r')
set(ax(1),'XLim',[X1(1) X2(end)])
set(ax(2),'XLim',[X1(1) X2(end)])
set(ax(1),'YLim',[0 1.05*max(Y1)])
set(ax(2),'YLim',[0 1.15*max(P2)])
4 Comments
Marco Uzielli
on 24 Jun 2014
Marco Uzielli
on 24 Jun 2014
Joseph Cheng
on 24 Jun 2014
I'm not entirely sure why its not showing up especially after you put in your coding error. I can't seem to reproduce it with any data sets i create. However i broke it down. Does this code work? i substituted the 2nd axis to be a marker such that i could see both plots if they are on top of each other.
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1);
box(axes1,'on');
hold(axes1,'all');
% Create plot
plot(X1,Y1,'Parent',axes1,'MarkerFaceColor',[0.49 1 0.63],...
'MarkerEdgeColor',[0 0 0],...
'MarkerSize',10);
% Create xlabel
xlabel('x','FontName','Arial');
% Create ylabel
ylabel('y1','Color',[0 0 1]);
% Create axes
xlim(axes1,[X1(1) X1(end)])
ylim(axes1,[0 1.05*max(Y1)])
axes2 = axes('Parent',figure1,'YAxisLocation','right','YColor',[0 0.5 0],...
'Color','none');
box(axes2,'on');
hold(axes2,'all');
% Create ylabel
ylabel('y2','VerticalAlignment','cap','Color',[0 .5 0]);
% Create plot
plot(X2,P2,'Parent',axes2,'MarkerSize',10,'Marker','+','LineStyle','none',...
'Color',[0 .5 0]);
xlim(axes2,[X1(1) X1(end)])
ylim(axes2,[0 1.05*max(P2)])
Marco Uzielli
on 24 Jun 2014
Categories
Find more on Two y-axis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
