在Mathematica绘图中强制x轴与y轴对齐

5
在Mathematica中,当我绘制图形时,有时候x轴并不总是与图形的底部完全对齐。有没有办法让它始终这样做?
以下是我所说的示例:http://i.imgur.com/3lcWd.png 我希望x轴能够与底部的零刻度线完美对齐,而不是像该图像中那样在y轴的中间。
我有办法做到这一点吗?

我认为如果您能在问题中添加生成该图/图像的Mathematica语句会有所帮助? - dbjohn
4个回答

9

5
以下内容将在左侧和底部绘制您的坐标轴,与坐标值无关:
aPlot[f_, var_, opts : OptionsPattern[]] :=
 Plot[f, var,
  AxesOrigin -> 
   First /@ (# /. AbsoluteOptions[Plot[f, var, opts], #] &@PlotRange), opts]

aPlot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, Filling -> Axis]

enter image description here

aPlot[Sin[x], {x, 0, 2 Pi}]

enter image description here


@belisarius:我喜欢你的解决方案和Alexey Popkov的派生解决方案,因为它们具有普适性,但我想避免使用它们,因为它们更加复杂,而我需要进行的对齐可以轻松地通过AxesOrigin -> {0, 0}来满足。很遗憾这样的东西还没有内置到Mathematica中。 - Mike Bailey
@Mike 如果你只需要 {0,0} 这种情况,显然 AxesOrigin 是最好的选择! - Dr. belisarius

4
你也可以使用类似以下方式:Frame -> {{Automatic, None}, {Automatic, None}} (另外,我认为它不默认选择{0,0}意味着y=0PlotRangePadding带入了范围内。因此,这可能是另一个需要关注的选项。)

1

这里是(在我看来)更优雅的方法,基于belisarius的代码,使用DisplayFunction选项(请参见此处关于该选项的有趣讨论):

Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, 
 Filling -> Axis, 
 DisplayFunction -> 
  Function[{plot}, 
   Show[plot, 
    AxesOrigin -> 
     First /@ (PlotRange /. AbsoluteOptions[plot, PlotRange]), 
    DisplayFunction -> Identity]]]

两种方法的唯一缺点是 AbsoluteOptions 并不总是给出正确的PlotRange。解决方案是使用 Ticks 方法(它提供了完整的PlotRange,并添加了显式值PlotRangePadding):
completePlotRange[plot_] := 
 Last@Last@
   Reap[Rasterize[
     Show[plot, Ticks -> (Sow[{##}] &), DisplayFunction -> Identity], 
     ImageResolution -> 1]]
Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, 
 Filling -> Axis, 
 DisplayFunction -> 
  Function[{plot}, 
   Show[plot, AxesOrigin -> First /@ completePlotRange[plot], 
    DisplayFunction -> Identity]]]

有趣的是,这段代码给出的渲染效果与简单指定 Frame -> {{Automatic, None}, {Automatic, None}}, Axes -> False 完全相同。
pl1 = Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, 
   Filling -> Axis, 
   DisplayFunction -> 
    Function[{plot}, 
     Show[plot, AxesOrigin -> First /@ completePlotRange[plot], 
      DisplayFunction -> Identity]]];
pl2 = Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, 
   Filling -> Axis, Frame -> {{Automatic, None}, {Automatic, None}}, 
   Axes -> False];
Rasterize[pl1] == Rasterize[pl1]

=> True

我知道关于PlotRange的异常,但我认为它与一些Plot[]的派生版本有关,而不是与Plot[]本身有关。 - Dr. belisarius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接