Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing x axis to align with y axis in Mathematica Plot

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: 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?

like image 600
Mike Bailey Avatar asked Sep 30 '11 20:09

Mike Bailey


4 Answers

Use the option AxesOrigin -> {0,0}

like image 193
Searke Avatar answered Jan 04 '23 01:01

Searke


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

like image 43
Dr. belisarius Avatar answered Jan 04 '23 01:01

Dr. belisarius


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

like image 29
Brett Champion Avatar answered Jan 03 '23 23:01

Brett Champion


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
like image 36
Alexey Popkov Avatar answered Jan 04 '23 01:01

Alexey Popkov