29 views (last 30 days)

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.

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...

dpb
on 9 Dec 2015

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/259371-matlab-graphs-arbitrary-x-axis#comment_328469

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/259371-matlab-graphs-arbitrary-x-axis#comment_328469

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/259371-matlab-graphs-arbitrary-x-axis#comment_328502

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/259371-matlab-graphs-arbitrary-x-axis#comment_328502

Sign in to comment.