MATLAB Graphs (Arbitrary X-Axis)
6 views (last 30 days)
Show older comments
Hello
I have two formulas as a function of y
Eo = 3.391 - 1.652.*y + 0.863.*y.^2-0.123.*y.^3 ;
Eg= 1.35-0.72*y+0.12*y.^2 ;
I plot two equations by varying
y= 0 : 0.01 : 0.8 ;
than plot (Eo,Eg) , which looks like the attached figure.
My question is: Can I put y at top x-axis ?
I want to put the values of y at top x-axis and dont want to plot any function against y.
Thanks in advance.
2 Comments
Walter Roberson
on 9 Dec 2015
Would the result have no labels on the left or right side, and would continue to have the 0.85 to 1.35 labels on the bottom, but as well on the top would have labels 2.5 to 3.4 ? And it would have no line drawn because you do not want to plot any function against y??
Accepted Answer
dpb
on 8 Dec 2015
Edited: dpb
on 9 Dec 2015
Oh, ok, I did misread the question, sorry...
hAx2=axes('Position',get(gca,'Position'),'XAxisLocation','top');
xlim(hAx2,[y(1) y(end)])
See the link under graphics on 'Using Multiple X- and Y-Axes' under the Graphics Objects section for more details.
NB: The above will not leave consistent tick spacing on the two axes in all likelihood; you can find a pleasing number of ticks and commensurate spacing between the two to allow you to line them up.
ADDENDUM
I forgot to let the first axes show thru the second, it is illustrated in the link referred to. Add 'Color', 'none' to the call creating the second axes so won't hide the first...
hAx2=axes('Position',get(gca,'Position'),'XAxisLocation','top', 'Color', 'none');
Also, you need to either add the legend while the first axes are current or get the axes handle and use the optional handle argument...after the call to plot, retrieve and save the current axes handle...
...
plot(....
hAx1=gca; % get handle for first axes
...
hAx2=axes(...
% do some stuff on this axes..
...
legend(hAx1, ... % now put legend on first axes...
Also, as noted earlier, you'll want to fix up the tick spacing...
set(hAx2,'xtick',linspace(y(1),y(end),11))
You can also use a format string with 'xticklabel' to write with consistent formatting to look a little better. I gotta' run; meeting in town...
OK am back; the latter would be something like--
set(hAx2,'xticklabel',num2str(get(hAx2,'xtick').','%0.2f'))
where the ticks were set as above so the two are in sequence as shown. One could also do the same as above for hAx1 to fixup the ugly default issue of differing format for label depending on whether is/is not the trailing zero and leading zero. I've ranted about this trivial-to-fix but unprofessional-looking plotting issue for almost 30 years (since Release 4.0) and TMW hasn't fixed it yet...
3 Comments
dpb
on 9 Dec 2015
No, "it" isn't empty, the first axes is hidden by the second by the order in which things were drawn. The "is empty" message is for the second set of axes. Two ways to fix...see updated Answer for code.
More Answers (0)
See Also
Categories
Find more on Title 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!