Using Multiple X- and Y-Axes doesn't work ?
4 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
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.)
See Also
Categories
Find more on Axis Labels 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!