3

I tried creating a plot with two YAxis like this:

x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
[hAx,hLine1,hLine2]=plotyy([x',x'],[y1',y2'],x,y3);

Now i have two problem with this code:

  1. I can change the Linestyles of the two hLines using hLine1.LineStyle = ':'; for example, but i can not change the styles of the two lines, that hLine1 consists of. Does anyone know how to do this?
  2. I can't use hLine2.YLim = [0 100] to manually adjust the y-limits shown on the 2nd y-axis.

After I couldn't solve the problem using the plotyy, I searched the MATLAB documentation and found another way of implementing my plot, which I thought might be easier to handle:

x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;

figure
hold on;
line(x,y1,'Color','r')
line(x,y2,'Color','y')
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right');
line(x,y3,'Parent',ax2,'Color','b')

The problem here is, that it doesn't even show the first and the second line, but only the third and i don't know why. I would prefer getting the problem solved using the plotyy, but if that's not possible I would appreciate a solution for the 2nd piece of code as well.

Jonathan H
  • 7,591
  • 5
  • 47
  • 80
Max
  • 1,471
  • 15
  • 37

2 Answers2

1

I think you haven't noticed that the outputs of plotyy are arrays of objects, and not single objects.

x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;

[hAx,hLine1,hLine2]=plotyy([x',x'],[y1',y2'],x,y3);
hLine1(1).LineStyle = '--';
hLine1(2).LineStyle = ':';

% either this
ylim( hAx(2), [0 110] );

% or alternatively
f=gcf; ylim( f.Children(2), [0 110] );
Jonathan H
  • 7,591
  • 5
  • 47
  • 80
  • Thank you and you are right, i didn't notice that, thus i could've imagined since matlab actually has arrays EVERYWHERE.... But the ylim thing doesn't work, matlab tells me "Function 'subsindex' is not defined for values of class 'matlab.graphics.axis.Axes'." for that `ylim()` line. – Max Sep 14 '15 at 10:17
  • @Max This is curious, what version of Matlab are you using? Alternatively, you could get the handle for each axes manually (see for instance http://stackoverflow.com/questions/3938348/matlab-how-to-obtain-all-the-axes-handles-in-a-figure-handle). – Jonathan H Sep 14 '15 at 10:50
  • @john i'm using Matlab R2015a. i will try your suggested alternative later this day, give feedback and probably accept your answer then! Thank you once again :) – Max Sep 14 '15 at 11:05
  • ok, the alternative works, but it doesn't do, what i wanted. i wanted to change the limits of the 2nd y-axis, how do i do that now? using f.children(3) does not work. – Max Sep 14 '15 at 13:48
  • and apart from that, i have another question which has to do with this question, but after all is not the same. Should i edit my question anyway, or ask a new question? – Max Sep 14 '15 at 13:51
  • @Max `hAx` is a 1x2 array of axes objects. Adjust the limits using `xlim(hAx(2), newlimits)`, `hAx(2).XLim = newlimits;`, or `set(hAx(2).XLim, newlimits);` – sco1 Sep 14 '15 at 14:02
1

You're not seeing the first two lines because axes backgrounds are white by default. Setting the Color property of the second axes object to 'none' should give you what you're looking for:

x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;

figure
hold on;
line(x,y1,'Color','r')
line(x,y2,'Color','y')
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color', 'none');
line(x,y3,'Parent',ax2,'Color','b')

the plot

EDIT: I'd also recommend checking out linkaxes if you're going to be zooming/panning your axes and want to keep some or all of the axes synchronized.

sco1
  • 12,154
  • 5
  • 26
  • 48
  • yes, this worked, but i'm still trying to go with the `plotyy`-solution provided by sh3ljohn. but what do you mean by "checking out" the linkaxes? like `linkaxes([ax1,ax2],'off');`? – Max Sep 14 '15 at 13:57
  • When you use `plotyy` it links your x-axes for you, so if you zoom or pan your plots it will update the limits of both axes. Utilizing the 2-axis object solution doesn't do this so if it's behavior you want then you'll need to set it yourself. – sco1 Sep 14 '15 at 13:58