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

Walter Roberson
Walter Roberson 2015년 12월 9일
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??
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

댓글을 달려면 로그인하십시오.

 채택된 답변

dpb
dpb 2015년 12월 8일
편집: dpb 2015년 12월 9일

1 개 추천

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

Abi Waqas
Abi Waqas 2015년 12월 8일
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.
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 2015년 12월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2015년 12월 8일

편집:

dpb
2015년 12월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by