【问题标题】:Forcing x axis to align with y axis in Mathematica Plot强制 x 轴与 Mathematica Plot 中的 y 轴对齐
【发布时间】:2011-09-30 20:56:12
【问题描述】:

在 Mathematica 中,当我绘制事物时,有时我并不总是让 x 轴与绘图的确切底部对齐。有什么办法可以强制它一直这样做吗?

这是我所说的一个例子:http://i.imgur.com/3lcWd.png

我希望 x 轴与底部的零刻度线完美对齐,而不是像在该图像中那样位于 y 轴的中间。

我有什么办法可以做到这一点?

【问题讨论】:

  • 我认为如果您可以将产生该图/图像的数学语句添加到您的问题中会有所帮助吗?

标签: wolfram-mathematica plot


【解决方案1】:

【讨论】:

    【解决方案2】:

    以下将在左侧和底部绘制您的轴,与坐标值无关:

    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]
    

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

    【讨论】:

    • @belisarius:我喜欢你的解决方案和 Alexey Popkov 的派生解决方案,因为它们具有普遍性,但我想远离它们,仅仅是因为它要复杂得多,而且我需要做的对齐很容易满足AxesOrigin->{0, 0}。太糟糕了,这样的东西还没有内置到 Mathematica 中。
    • @Mike 如果您只需要 {0,0} 案例,那么显然AxesOrigin 是最好的选择!
    【解决方案3】:

    你也可以使用类似:Frame -> {{Automatic, None}, {Automatic, None}}

    (另外我认为默认情况下它没有选择{0,0},这意味着y=0 正在被PlotRangePadding 纳入范围。所以这可能是另一个值得关注的选项。)

    【讨论】:

      【解决方案4】:

      这是 (IMO) 基于使用 DisplayFunction 选项的贝利撒留代码的更优雅的方法(请参阅 here 关于此选项的有趣讨论):

      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 does not always give correct value of PlotRange。解决方案是使用the Ticks hack(它给出了完整的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[] 本身无关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多