强制 Mathematica 图中的 x 轴与 y 轴对齐

发布于 2024-12-07 11:45:07 字数 271 浏览 1 评论 0原文

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

这是我正在谈论的示例: https://i.sstatic.net/dJPkg.png< /a>

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

我有什么办法可以实现这个目标吗?

In Mathematica, when I plot things sometimes I don't always get the x-axis to line up with the exact bottom of the plot. Is there any way I can force it to do this all the time?

Here's an example of what I'm talking about: https://i.sstatic.net/dJPkg.png

I want the x-axis to line up perfectly with the zero tick mark way at the bottom, not in the middle of the y-axis as it is in that image.

Any way I can accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

埋葬我深情 2024-12-14 11:45:07

以下内容将在左侧和底部绘制轴,无论坐标值如何:

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}]

在此处输入图像描述

The following will draw your Axes on the left and bottom, irrespective to the coordinate values:

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

人生百味 2024-12-14 11:45:07

您还可以使用类似的内容:Frame -> {{Automatic, None}, {Automatic, None}}

(另外我认为默认情况下不选择 {0,0} 意味着 y=0< /code> 正在被 PlotRangePadding 纳入范围,因此这可能是另一个需要关注的选项。)

You could also use something like: Frame -> {{Automatic, None}, {Automatic, None}}

(Also I think that fact that it's not choosing {0,0} by default means that y=0 is being brought into range by PlotRangePadding. So that may be another option to keep an eye on.)

赠意 2024-12-14 11:45:07

这是(IMO)基于 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 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 ->; 完全相同。 {{自动,无},{自动,无}},轴 ->错误

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

Here is (IMO) more elegant method based on belisarius's code which uses the DisplayFunction option (see here interesting discussion on this option):

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]]]

The only drawback of both methods is that AbsoluteOptions does not always give correct value of PlotRange. The solution is to use the Ticks hack (which gives the complete PlotRange with explicit value of PlotRangePadding added):

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]]]

It is interesting to note that this code gives exactly the same rendering as simply specifying 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文