MATLAB Answers

MATLAB Graphs (Arbitrary X-Axis)

29 views (last 30 days)
Abi Waqas
Abi Waqas on 8 Dec 2015
Edited: dpb on 9 Dec 2015
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
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??
Abi Waqas
Abi Waqas on 9 Dec 2015
Dear Walter
thank for the comment. I have attached the new graph image.
Here is my code to generate that Image.
y= 0:0.01:0.8;
Eo = 3.391 - 1.652.*y + 0.863.*y.^2-0.123.*y.^3 ;
Ed = 28.91 - 9.278.*y + 5.626.*y.^2 ;
Eg= 1.35-0.72*y+0.12*y.^2 ; %At 300 Kelvin
p1_Eo= 1.894 ;
p2_Eo = 0.8116 ;
p1_Ed = 5.193 ;
p2_Ed = 11.24 ;
Eo_fit = p1_Eo.*Eg+p2_Eo ;
Ed_fit = p1_Ed.*Eo_fit+p2_Ed;
figure (1)
plot(Eg,Eo,'b',Eg,Eo_fit,'r');
grid on ;
title ('Original Eo v/s Fit Eo with Eg' );
xlabel ('Eg') ; ylabel ('Eo');
legend ('Orignal Eo','Fit Eo');
I want exactly same graph but having y vector at top x-axis.
I hope my question is clear.
Thanks

Sign in to comment.

Accepted Answer

dpb
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

Abi Waqas
Abi Waqas on 8 Dec 2015
Dear dbp
Thanks for reply.
I think i coudn't clear my question.
My question was to keep Eg at x axis (as shown in figure), Eo at y axis (as shown in figure) also I want y at top x axis.
I hope my question is clear now.
Abi Waqas
Abi Waqas on 9 Dec 2015
y= 0:0.01:0.8;
Eo = 3.391 - 1.652.*y + 0.863.*y.^2-0.123.*y.^3 ;
Ed = 28.91 - 9.278.*y + 5.626.*y.^2 ;
Eg= 1.35-0.72*y+0.12*y.^2 ; %At 300 Kelvin
p1_Eo= 1.894 ; %p1 for Eo vs Eg (1.863, 1.925)
p2_Eo = 0.8116 ; %p2 for Eo vs Eg (0.7747, 0.8484)
%p1_Ed = 9.817 ; %p1 for Ed vs Eg (9.562, 10.07)
%p2_Ed = 15.48 ; %p2 for Ed vs Eg (15.18, 15.78)
p1_Ed = 5.193 ; %p1 for Ed vs Eo (5.144, 5.242)
p2_Ed = 11.24 ; %p2 for Ed vs Eo (11.09, 11.39)
Eo_fit = p1_Eo.*Eg+p2_Eo ;
Ed_fit = p1_Ed.*Eo_fit+p2_Ed;
figure (1)
plot(Eg,Eo,'b',Eg,Eo_fit,'r');
grid on ;
hAx2=axes('Position',get(gca,'Position'),'XAxisLocation','top');
xlim(hAx2,[y(1) y(end)])
title ('Original Eo v/s Fit Eo with Eg' );
xlabel ('Eg') ; ylabel ('Eo');
legend ('Orignal Eo','Fit Eo');
Here is code, I tried to implement your suggestion also I have used line command to do that but not getting the expected results.
In your suggestion I am getting the correct plot but it empty.
If you can help please.
dpb
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!