Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Bug introduced in 10.0.0 and fixed in 10.0.1

The Plot option GridLines can be specified either as a list of explicit values at which to draw the lines, or as a function that calculates the grid lines based on the maximum and minimum values along each axis. In Mathematica version 8, I was able to specify this function for GridLines in two equivalent ways:

gridLineFunction[xmin_, xmax_] := {.1, .2, .5};

Plot[Sin[x], {x, 0, 1}, GridLines -> gridLineFunction]


This follows the example in the documentation, but for simplicity it returns a fixed list of numbers without doing any calculations at all. However, now I rewrite the above by passing it directly to GridLines as a Function:

Plot[Sin[x], {x, 0, 1}, 
 GridLines -> Function[{xmin, xmax}, {.1, .2, .5}]]

no lines

This last line is what I get in version 10 on OS X. It used to work in version 8. And the equivalent construct still works for the Ticks option:

Plot[Sin[x], {x, 0, 1}, Ticks -> Function[{xmin, xmax}, {.1, .2, .5}]]


Is there any reason I'm overlooking why this Function approach no longer works in GridLines, or is this a bug?

share|improve this question
In version 8.0.4 under Win7 x64 your code with GridLines -> Function[...] gives this. Looks like a bug in v.10.0.0. –  Alexey Popkov Jul 11 '14 at 19:14
Works fine on v8 Linux as well. –  Öskå Jul 11 '14 at 19:19
Ouch. It has been reported. –  rcollyer Jul 11 '14 at 20:00
@rcollyer I do hope point release updates are still included with Home Edition. :-O –  Mr.Wizard Jul 11 '14 at 20:01
@Mr.Wizard yes. But, I don't have specific dates, nor can I talk about them. Soon, I suspect. –  rcollyer Aug 1 '14 at 19:27

1 Answer 1

up vote 5 down vote accepted

In the meantime, here is a way to repair the gridlines

fixGridLines[plot : _Graphics | _Legended] :=
  Module[{xmin, xmax, ymin, ymax},
   {{xmin, xmax}, {ymin, ymax}} = 
    Through[{Min, Max}[#]] & /@ 
     Transpose@Cases[plot, {_Real, _Real}, Infinity];
   With[{p = plot}, MakeBoxes[p, StandardForm]] /. 
    pat : (GridLines -> {Charting`ScaledTickValues[_, xf_[_]] &, 
         Charting`ScaledTickValues[_, yf_[_]] &}) :> 
     GridLines -> {xf[xmin, xmax], yf[ymin, ymax]}
   ] // ToExpression


plot = Plot[Sin[x], {x, 0, 1}, 
   GridLines -> Function[{xmin, xmax}, {.1, .2, .5}]];

Mathematica graphics

You can examine the box form of the plot to see where the GridLines functions got stored.

Edit: Other ways to get the plot range.

This includes PlotRangePadding in the computation (might be best of all):

{{xmin, xmax}, {ymin, ymax}} = 
  Charting`get2DPlotRange[Replace[plot, p_Legended :> First@p]];

This does not include PlotRangePadding:

{{xmin, xmax}, {ymin, ymax}} = With[{p = plot},
   PlotRange /. Cases[MakeBoxes[p, StandardForm], pat : (PlotRange -> _), Infinity]
share|improve this answer
Yes, this is a work-around. I would just include Legended as an allowed Head for the plot argument, because otherwise it won't work with the PlotLegends options. –  Jens Aug 6 '14 at 21:00
@Jens Thanks for the tip. Legended now included. –  Michael E2 Aug 6 '14 at 22:32
I accepted this because that's probably the best we can do - after all, it's still a bug... but now it's clearer what caused it. –  Jens Aug 6 '14 at 22:36
@Jens Thanks, I realize it is not a complete explanation. I just thought it might be helpful for others. –  Michael E2 Aug 7 '14 at 0:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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