Creating a second X-axis

조회 수: 12 (최근 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