What's wrong with Plotyy?

1 次查看(过去 30 天)
Markus
Markus 2014-4-4
评论: dpb 2014-4-5
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 个评论
dpb
dpb 2014-4-4
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.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2014-4-5
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 个评论
Markus
Markus 2014-4-5
Hi, thanks for the answer.
It is quite close to what I am looking for, but your code displays 4 triangles on the plot, while I just need to visualize 2 triangles. How can I do that?
dpb
dpb 2014-4-5
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Two y-axis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by