Creating a second X-axis
Show older comments
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
Azzi Abdelmalek
on 22 Jun 2013
What is your question?
dpb
on 22 Jun 2013
The relationship is inherent in handle graphics and the doc's are pretty good about explaining them. Start w/ the sections on Graphics and specifically 2D graphics. Look at doc for
doc gca doc get % and set and friends...
Follow the example for multiple axes at
Michael
on 23 Jun 2013
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.
Answers (1)
Walter Roberson
on 23 Jun 2013
0 votes
You might want to use the FEX contribution "plotxx"
Categories
Find more on Data Exploration 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!