How do I make both plots visible using plotyy?

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.

Réponses (1)

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 commentaires

Marco Uzielli
Marco Uzielli le 24 Juin 2014
Hi Joseph,
The ranges for Y1 and Y2 are, respectively, approximately 0 to 0.14 and 0 to 20. X1 and X2 are almost perfectly overlapping. I actually use the same Xlims for both lines. Please note that if I purposely include an error in the code before the plot is completed, I am able to see both lines in the "work in progress" figure which appears when I get the error message.
Marco Uzielli
Marco Uzielli le 24 Juin 2014
Please see the completed plot:
and the incomplete plot, which appears when the routine is interrupted due to a purposely included coding error:
The coding error is included just before the specification of the Ylims for the second line:
set(ax(2),'YLim',[0 1.05*max(Y2)])
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
Marco Uzielli le 24 Juin 2014
Thanks, Joseph. I tried pasting your code but am still only able to see the second plot (the green one) while the first plot disappears.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by