Creating a second X-axis
显示 更早的评论
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 个评论
Azzi Abdelmalek
2013-6-22
What is your question?
dpb
2013-6-22
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
2013-6-23
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
2013-6-23
0 个投票
You might want to use the FEX contribution "plotxx"
类别
在 帮助中心 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!