Creating a second X-axis

9 次查看(过去 30 天)
Michael
Michael 2013-6-22
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 个评论
Michael
Michael 2013-6-23
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 2013-6-24
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.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2013-6-23
You might want to use the FEX contribution "plotxx"
  3 个评论
Michael
Michael 2013-6-24
编辑:Michael 2013-6-24
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Exploration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by