15
$\begingroup$

When plotting with Frame->True option, the axis is hidden when it is too close to the boundary of the plot. For example:

Plot[Sin[x], {x, 0, 10}, PlotRange -> {-0.1, 1}, Frame -> True]

X-axis is hidden

But if I increase the range, the x-axis can appear.

Plot[Sin[x], {x, 0, 10}, PlotRange -> {-0.2, 1}, Frame -> True]

X-axis is not hidden

How to force Mathematica to show the X-axis in the first case without changing the plotting range?

$\endgroup$
3
  • $\begingroup$ I have tried assigning AxesOrigin -> {0, 0} but it doesn't work. $\endgroup$
    – Louis Yang
    Commented Mar 5, 2017 at 0:10
  • $\begingroup$ Thanks for the check, couldn't try myself from a phone. $\endgroup$
    – Kuba
    Commented Mar 5, 2017 at 12:24
  • $\begingroup$ Inversely related: (32189) $\endgroup$
    – Mr.Wizard
    Commented Mar 10, 2017 at 14:09

3 Answers 3

18
$\begingroup$

We can easily show that this is not a feature of Plot but of Graphics itself:

Graphics[Circle[], Frame -> True, Axes -> True, AxesOrigin -> {0, 0}, 
   PlotRange -> {{0, 1}, {#, 1}}] & /@ {-1/9, -1/9 - 0.00000001}

enter image description here

So it seems that the axis line is suppressed at 1/9 of the range of the plot. This holds in both directions and at other scales:

Graphics[
   {Circle[], Circle[{2, 2}, 2]}, Frame -> True, Axes -> True, 
   AxesOrigin -> {0, 0}, PlotRange -> {2 {#, 1}, 4 {#, 1}}
] & /@ {-1/9, -1/9 - 0.00000001}

enter image description here

I suspect that this was intentionally programmed into Graphics to reduce plot clutter.

I recommend the use of GridLines in its place:

Plot[Sin[x], {x, 0, 10}, PlotRange -> {-0.1, 1}, Frame -> True, 
 GridLines -> {{0}, {0}}, GridLinesStyle -> Black]

enter image description here

$\endgroup$
4
+200
$\begingroup$

You can use an AxisObject in the Prolog option to add the x-axis without adjusting PlotRange.

Plot[Sin[x], {x, 0, 10}
 , PlotRange -> {-0.1, 1}
 , Frame -> True
 , Prolog -> {AxisObject[{"Horizontal", 0}, TickLabels -> None]}
 ]

Plot with x-axis

To show the tick labels remove option TickLabels -> None.

To remove ticks replace option TickLabels -> None with Ticks -> {}.

AxisObject is marked experimental in 14.1 but has been in around since 12.3.

Hope this helps.

$\endgroup$
2
  • 1
    $\begingroup$ I now realize that the early developers of Mma didn't now what they were doing when they named line objects Line, point objects Point, etc. (+1 earlier) $\endgroup$
    – Michael E2
    Commented Sep 3 at 13:25
  • $\begingroup$ That was my thought as well, XXObject sounds weird in Mathematica. I did not know about AxisObject. While it's not a perfect solution, it's quite intuitive, and I don't think a perfect one exists. I'll award the bounty now, so I wouldn't forget before it expires. Thank you to both for brainstorming solutions! $\endgroup$
    – Szabolcs
    Commented Sep 6 at 11:14
3
$\begingroup$

I know FullGraphics complains like a spoiled kid, and I don't completely trust it; but this seems to work:

Show[Quiet@FullGraphics@#, Options@#] &@
 Plot[Sin[x], {x, 0, 10}, PlotRange -> {-0.05, 1}, Frame -> True]

As for the criterion in the bounty, "suitable for a classroom setting," I feel one will have to do something arcane, unless one can find a suitable option. Maybe this is slightly better:

SetOptions[Plot, 
  DisplayFunction -> (Show[Quiet@FullGraphics@#, Options@#] &)];
Plot[Sin[x], {x, 0, 10}, PlotRange -> {-0.05, 1}, Frame -> True]

You set the option once and all the Plot commands will show the axes and frame.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.