两轴曲线,其中一条曲线不完整

时间:2017-03-10 11:18:08

标签: plot wolfram-mathematica

我使用TwoAxisPlot组合来自NDSolve的函数的两个图,但结果有一条曲线被截断,因此不完整。在定义TwoAxisPlot函数时,我不太了解所有的术语,所以我不知道问题的根源。代码如下:

a = 0.99*10^-9;
b = 0.24*10^-3;
d = 1.21*10^-3;
T0 = 1*10^6;
n0 = 0.9*10^9;
ti = -20;
tf = 500;
kB = 1.38*10^-16;

Qb = 0.33*10^-3;
sig = 1;
var = sig^2;
Ag = 16.5;

Qg = Ag* Exp[-(t - 10)^2/(2*var)];
Qgt = Qg + Qb;

sss = NDSolve[{T'[t] == -(n[t]^-1) T[t]^(7/2) (a) - 
  n[t] T[t]^(-1/2) (b) + Qgt/(2*kB*n[t]), 
n'[t] == T[t]^(5/2) (a) - (n[t]^2) (T[t]^(-3/2)) (d), T[ti] == T0,
 n[ti] == n0}, {T, n}, {t, ti, tf}];

这给了我两个插值函数,我可以完全单独绘制:

TP = Plot[T[t] /. sss, {t, ti, 300}, PlotRange -> All];
TPPa = Show[TP, Frame -> True, 
FrameLabel -> {{"Temperature, K", ""}, {"Time, s", ""}}]

NP = Plot[n[t] /. sss, {t, ti, 300}, PlotRange -> All];
NPPa = Show[NP, Frame -> True, 
FrameLabel -> {{"Density, \!\(\*SuperscriptBox[\(cm\), \(-3\)]\)", 
 ""}, {"Time, s", ""}}]    

enter image description here enter image description here

然后我定义了从这个网站复制的未更改的TwoAxisPlot函数:Wolfram Documentation Center

TwoAxisPlot[{f_, g_}, {x_, x1_, x2_}] := 
Module[{fgraph, ggraph, frange, grange, fticks, 
gticks}, {fgraph, ggraph} = 
MapIndexed[
Plot[#, {x, x1, x2}, Axes -> True, 
  PlotStyle -> ColorData[1][#2[[1]]]] &, {f, g}]; {frange, 
grange} = (PlotRange /. AbsoluteOptions[#, PlotRange])[[
  2]] & /@ {fgraph, ggraph}; fticks = N@FindDivisions[frange, 5];
gticks = 
Quiet@Transpose@{fticks, 
  ToString[NumberForm[#, 2], StandardForm] & /@ 
   Rescale[fticks, frange, grange]};
Show[fgraph, 
ggraph /. 
Graphics[graph_, s___] :> 
 Graphics[
  GeometricTransformation[graph, 
   RescalingTransform[{{0, 1}, grange}, {{0, 1}, frange}]], s], 
Axes -> False, Frame -> True, 
FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}}, 
FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}]]

并使用它

TwoAxisPlot[{T[t] /. sss, n[t] /. sss}, {t, -20, 300}]

enter image description here

但是情节现在被截断,并且没有显示完整的曲线。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

添加PlotRange -> Full,如下所示。

TwoAxisPlot[{f_, g_}, {x_, x1_, x2_}] := Module[
  {fgraph, ggraph, frange, grange, fticks, gticks},
  {fgraph, ggraph} = MapIndexed[Plot[#, {x, x1, x2}, Axes -> True, 
      PlotStyle -> ColorData[1][#2[[1]]], PlotRange -> Full] &, {f, g}];
  {frange, grange} = (PlotRange /. AbsoluteOptions[#, PlotRange])[[2]] & /@
    {fgraph, ggraph};
  fticks = N@FindDivisions[frange, 5];
  gticks = Quiet@Transpose@{fticks, ToString[NumberForm[#, 2],
          StandardForm] & /@ Rescale[fticks, frange, grange]};
  Show[fgraph, ggraph /. Graphics[graph_, s___] :> Graphics[
      GeometricTransformation[graph, RescalingTransform[{{0, 1}, grange},
        {{0, 1}, frange}]], s], Axes -> False, Frame -> True, 
   FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}}, 
   FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}]]

Labeled[
 TwoAxisPlot[{T[t] /. sss, n[t] /. sss}, {t, -20, 300}],
 {Rotate["Temperature, K", Pi/2], "Time, s",
  Rotate["Density, \!\(\*SuperscriptBox[\(cm\), \(-3\)]\)", Pi/2]},
 {Left, Bottom, Right}]

enter image description here