开发者

Forcing x axis to align with y axis in Mathematica Plot

开发者 https://www.devze.com 2023-04-10 10:04 出处:网络
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 t开发者_如何学运维o do this all the time?

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 t开发者_如何学运维o do this all the time?

Here's an example of what I'm talking about: http://i.imgur.com/3lcWd.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?


Use the option AxesOrigin -> {0,0}


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]

Forcing x axis to align with y axis in Mathematica Plot

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

Forcing x axis to align with y axis in Mathematica Plot


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.)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号