14
$\begingroup$

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:

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

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

lines

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

ticks

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

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

1 Answer 1

6
$\begingroup$

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

Example:

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

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]
   ]
$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Jens
    Aug 6, 2014 at 21:00
  • 1
    $\begingroup$ @Jens Thanks for the tip. Legended now included. $\endgroup$
    – Michael E2
    Aug 6, 2014 at 22:32
  • $\begingroup$ 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. $\endgroup$
    – Jens
    Aug 6, 2014 at 22:36
  • $\begingroup$ @Jens Thanks, I realize it is not a complete explanation. I just thought it might be helpful for others. $\endgroup$
    – Michael E2
    Aug 7, 2014 at 0:42

Your Answer

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

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