Using Multiple X- and Y-Axes doesn't work ?

2 views (last 30 days)
Tobias
Tobias on 21 Dec 2011
Hey everybody!
I'm trying to plot two datasets in one axes. So, I read the matlabs online documentation:
The code example on that page:
x1 = [0:.1:40];
y1 = 4.*cos(x1)./(x1+2);
x2 = [1:.2:20];
y2 = x2.^2./x2.^3;
%Using low-level line and axes routines allows you to superimpose objects easily. Plot the first data, making the color of the line and the corresponding x- and y-axis the same to more easily associate them.
hl1 = line(x1,y1,'Color','r');
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
%Next, create another axes at the same location as the first, placing the x-axis on top and the y-axis on the right. Set the axes Color to none to allow the first axes to be visible and color code the x- and y-axis to match the data.
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k');
%Draw the second set of data in the same color as the x- and y-axis.
hl2 = line(x2,y2,'Color','k','Parent',ax2);
The content is exactly what I want!!!
BUT:
I opened matlab with a blank m-file, copied the code from the documentation and ran it. It was working. So, I copied exactly the same code into the opening function of a figure from GUIDE, added an axes, called "axes1", to the figure, saved it an ran that code.
The result was that a figure opened and only the red dataset was drawn. No second dataset, no axis on the right, no axis on top.
I can't figure out what the problem is!
What's the difference between those cases?
Or what do I have to change to make it work?

Answers (1)

Sean de Wolski
Sean de Wolski on 21 Dec 2011
hl2 = line(x2,y2,'Color','k','Parent',ax2);
You're trying to put the line on parent axes ax2 which does not exist. Does changing ax2 tyo ax1 give you what you want? If not, do you need to create a second axes?
(It's not working on my command window either. I'd guess you had an ax2 lying around when it was working.)
  1 Comment
Tobias
Tobias on 21 Dec 2011
Hey!
I changed the line to:
hl2=line(x2,y2,'Color','k','Parent',ax1);
like you mentioned. The result is that the red dataset is shown like before but additionaly the black dataset is shown as well. But the black dataset isn't fitting to the dimensions of the axes(dataset ist too small) and the axis on top and at the right side are still missing too.
You said that I need another axes to be parent to the line but the function "axes(...);" creates an new axes!?
Since I wrote this question I was trying several things to get closer to the real problem. Sometime I deleted "'Position',get(ax1,'Position')" in the following line and took a look what might happen. Without this parameter the ax2 is shown in the figure. But not "overlapping" like they describe in the documentation... .
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right',
'Color','none','XColor','k','YColor','k');
So, I Think we are getting closer to the real problem. Do you have another idea ???

Sign in to comment.

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!