Stats

Popular Posts

Followers

Mathematica 教學 How to create a two-axis graph

戴忠淵 於 2011年9月15日星期四 上午12:46 發表


myplot[{f_,g_},range_List,color_List]:=
Block[{p1,p2,a1,a2,a3,b1,b2,myrange=range,mycolor=color},
p1=Plot[f,{x,myrange[[1]],myrange[[2]]},Axes->False,Frame->True];
p2=Plot[g,{x,myrange[[1]],myrange[[2]]},Axes->False,Frame->True];
a1=AbsoluteOptions[p1,FrameTicks][[1,2,2]];
a2=AbsoluteOptions[p2,FrameTicks][[1,2,2]];
b1=p1[[-1,4,-1,-1]];
b2=p2[[-1,4,-1,-1]];
a3=MapThread[
Insert[#1,#2,1]&,{a2[[All,2;;-1]],
b1[[1]]+((b1[[2]]-b1[[1]])/(b2[[2]]-b2[[1]])*(#-
b2[[1]]))&/@a2[[All,1]]}];

Plot[{f,b1[[1]]+((b1[[2]]-b1[[1]])/(b2[[2]]-b2[[1]])*
(g-b2[[1]]))},{x,myrange[[1]],myrange[[2]]},
PlotStyle->{{Thickness[0.01],mycolor[[1]]},
{Thickness[0.01],mycolor[[2]]}},
Axes->False,Frame->True,
FrameTicks->{{a1,a3},{Automatic,None}},
ImageSize->500]]

myplot[{0.25 Cos[x], 0.5 + Abs@Sin[x]}, {0, 2 Pi}, {Red, Blue}]
Tags:

讀者回應 ( 0 意見 )

發佈留言

Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.

如果這篇文章對你有幫助,那請留個訊息給我~