What's wrong with Plotyy?
1 view (last 30 days)
Show older comments
Hi All,
I am trying to use plotyy to plot a x-data value against 2-y values. However, the data are not displayed correctly on the secondary y-axis, as you can see from the script below. Specifically, I expect the black triangle having a value of -2 and the red triangle a value of 2 on the secondary y axis, while they are at -1 and 0.5, respectively.
Do you know what is wrong in my script?
clf
[AX,H1,H2] = plotyy(120, 1.5, 120, -2);
set(H1,'linestyle','none','marker','^','color','k','MarkerEdgeColor','k','MarkerFaceColor','k','Markersize',8);
set(AX(1),'Xlim',[110 170],'XTick',[110:10:170])
set(AX(2),'Xlim',[110 170],'XTick',[110:10:170])
set(AX(1),'Ylim',[0.5 4.5],'YTick',[0.5:.5:4.5])
set(AX(2),'Ylim',[-2 2],'YTick',[-2:.5:2])
set(AX(1),'ycolor','k')
set(AX(2),'ycolor','k')
set(AX(1),'box','off')
hold on
[AX,H1,H2] = plotyy(112, 3, 112, 2);
set(H1,'linestyle','none','marker','^','color','k','MarkerEdgeColor','k','MarkerFaceColor','r','Markersize',8);
set(AX(1),'Xlim',[110 170],'XTick',[110:10:170])
set(AX(2),'Xlim',[110 170],'XTick',[110:10:170])
set(AX(1),'Ylim',[0.5 4.5],'YTick',[0.5:.5:4.5])
set(AX(2),'Ylim',[-2 2],'YTick',[-2:.5:2])
set(AX(1),'ycolor','k')
set(AX(2),'ycolor','k')
Thank you!
1 Comment
dpb
on 4 Apr 2014
Couple of big things for starters...
A) you call plotyy the first time (actually both times) with only a single point for each--what's the point in that? Create the x,y vectors you wish and plot them in "one swell foop"
B) you then wipe out the handles for the first two axes in the second call by saving a new set of handles into the old variables thus wiping out access (easily) to the previous axes.
I don't have time to play at the moment but start by defining the data to plot for each axis as two vectors and plot them and do NOT call plotyy a second time at all--just make modifications needed to the two axes you already have.
Answers (1)
dpb
on 5 Apr 2014
Is this what you're looking for, roughly?
X=[120 120;112 112];Y= [1.5 -2;3 2]; % set up the point vectors
[AX,H1,H2] = plotyy(X(:,1),Y(:,1),X(:,2),Y(:,2),@scatter,@scatter); % scatter plot 'em
Rest is just formatting what you gots...
ylim(AX(1),[0.5 4.5])
set(AX(1),'ytick',[0.5:0.5:4.5])
ylim(AX(2),[-2 2])
set(H1,'marker','^','markeredgecolor','k','markerfacecolor','k')
set(H2,'marker','^','markeredgecolor','r','markerfacecolor','r')
2 Comments
dpb
on 5 Apr 2014
Only plot the ones you want, then. You've got two for each axis as you've defined the data sets; I just copied the data you had in the two calls. Delete whichever two you don't want from the X, Y arrays.
See Also
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!