Creating a second X-axis

20 views (last 30 days)
Michael
Michael on 22 Jun 2013
I would like to produce a second x-axis on a plot and I am having difficulty doing it. From the net resources I have have found the following code.
x1 = [0:.1:40];
y1 = 4.*cos(x1)./(x1+2);
x2 = [1:.2:20];
y2 = x2.^2./x2.^3;
hl1 = line(x1,y1,'Color','c');
ax1 = gca;
set(ax1,'XColor','b','YColor','r')
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
hl2 = line(x2,y2,'Color','k','Parent',ax2);
However I am unable to rewrite it to function for the plot I am using. There appears to be a relationship between the elements 'gca', 'get' , 'ax1' und 'ax2' that I do not understand. Also, all the web sites that I have found on the net so far seem to be written with the assumption that the relationship between These elements is understood.
  4 Comments
Michael
Michael on 23 Jun 2013
Hi dpb, I read These documents have only have basic understanding of These functions.
I only Need another x-axis on the top...not another Y axis on the right. When I try to cut out the 'YAxislocation', 'right' object and location elements, the Y Axis gets double printed on the left.
Consider the following code: x1= 1:20; x2=linspace(1,5,10) y=cos(x1);
hl1 = line(x1,y,'Color','r'); ax1 = gca;
That will print a cosine graph using the x1 values.
Now what I Need to do is to get Matlab to print another scale on the top of the graph: x2= linspace (1,5,10)
I do not want another line in the graph. Just a scale that I can define on the top of the graph. I have tried various combinations of the "x2" in the example but have not been successful.
dpb
dpb on 24 Jun 2013
Well, the very next thing the example I gave you the link to above does after drawing a line is...
_ 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');
That does precisely what you asked for...you can have used plot() before or whatever to get the first axis.
Modify colors, xlimits, whatever as you wish after you have the axis handle.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 23 Jun 2013
You might want to use the FEX contribution "plotxx"
  3 Comments
Michael
Michael on 24 Jun 2013
Edited: Michael on 24 Jun 2013
Hi Tom,
This is written on the origional post:
plotxx.m
by Denis Gilbert
25 Jan 1999 (Updated 19 Feb 2002)
When I try "doc plotxx.m" in the command window I get the same result as for plotxx. Matlab tells me the document does not exist.

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!