15
$\begingroup$

Bug introduced in 10.0 and fixed in 10.4


In this post @Heike provides this code:

ticksfun[xmin_, xmax_] := 
 Table[{10^i, Superscript[10, i]}, {i, Floor[Log10[xmin]], 
   Ceiling[Log10[xmax]]}]

LogLogPlot[Log[x!], {x, 1, 10^5}, 
 PlotRange -> {{0, 10^5}, {10^-1, 10^6}}, 
 Ticks -> {ticksfun, ticksfun}]

This works perfectly in v9.: enter image description here

But in v10, it does not: enter image description here

The error is:

Tick specification must be a list or a function

The help page for Ticks looks identical in v10 and v9. So the main question, is this a bug or is there an undocumented change in v10?

Note: Ticks in Plot seems to work relatively ok in v10.

Update

Based on the analysis provided by @rcollyer, I did some tests between Mathematica v9 and v10:

minmax = {};
ticksfun[xmin_, xmax_] := Module[{i},
   minmax = {xmin, xmax};
   Table[{10^i, Superscript[10, i]},
    {i, Floor[Log10[xmin]], Ceiling[Log10[xmax]]}]
   ];
minmax2 = {};
ticksfun2[xmin_, xmax_] := Module[{i},
   minmax2 = {xmin, xmax};
   Table[i, {i, Floor[xmin], Ceiling[xmax]}]
   ];

Then I run:

LogLogPlot[x^2, {x, 1, 10}, Ticks -> {ticksfun, ticksfun}]
Plot[x^2, {x, 1, 10}, Ticks -> {ticksfun2, ticksfun2}]

The first plot has error, the second is ok, fine, so what does Mathematica v9 return on minmax:

In[13]:= minmax
minmax2

Out[13]= {1., 100.}

Out[14]= {-2.08333, 102.083}

What does Mathematica v10.4 return on minmax?

In[101]:= minmax
minmax2

Out[101]= {-0.511686, 5.11686}

Out[102]= {-5.55556, 105.556}

I am not experienced in the internals of Mathematica but all this suggests that there is a change in what the plotting functions are sending to ticks fucntions. And notice the change is not only in LogLogPlot but in Plot as well. I think Mathematica should not be passing negative numbers in LogLogPlot.

Update 2

When I tried to implement the solution by @rcollyer, I realized, it is necessary to to further conversions. The solution posted at the bottom creates wrong descriptions of the axis --- the ticks are misplaced. The problem is that the range is supplied in natural logarithm while, we are placing log10 descriptions. At this moment I have something like:

ticksnofun[xmin_, xmax_] := Module[{i, xmine, xmaxe, xmin10, xmax10},
   {xmine, xmaxe} = {xmin, xmax};
   {xmin10, xmax10} = (#/Log[10]) & /@ {xmine, xmaxe};
   {xmin10, xmax10} = {Ceiling[xmin10], Floor[xmax10]};
   tcks10 = Table[i, {i, xmin10, xmax10}];
   tckse = (#/Log10[E]) & /@ tcks10;
   Transpose[{tckse, Superscript[10, #] & /@ tcks10}]
   ];

I am curious whether this could be written in a "better" Mathematica way (yet keeping it readable)?

$\endgroup$
8
  • $\begingroup$ Definitely looks like a bug. Maybe related to the dynamic magic giving me trouble here. $\endgroup$
    – Jens
    Apr 1, 2016 at 18:03
  • $\begingroup$ Plot doesn't work from either. $\endgroup$
    – BlacKow
    Apr 1, 2016 at 18:04
  • 1
    $\begingroup$ It's not a bug in Plot nor LogLogPlot. Posting answer now. $\endgroup$
    – rcollyer
    Apr 1, 2016 at 18:05
  • 2
    $\begingroup$ You're correct. It is not a bug, per se, in the ticks function, but what is being passed to the ticks function has changed, as I noted in my answer. So, the original ticks function must be modified: LogLogPlot is now passing scaled values, i.e. they are the Log of the range. There is a bug in LogLogPlot in that a ticks function is not accepted at all between 10.0 and 10.3. That has been mostly fixed. $\endgroup$
    – rcollyer
    Apr 4, 2016 at 17:46
  • 1
    $\begingroup$ Natural log, same base, internally, as LogLogPlot. $\endgroup$
    – rcollyer
    Apr 4, 2016 at 17:56

1 Answer 1

12
$\begingroup$

The advice below is for 10.4 and above. It appears there is a bug in 10.3 and lower.


On the surface, it looks like a bug. But, it is a bug in ticksfun. To see why, we need to see what is being passed into it, so we modify it as follows:

minmax = {};
ticksfun[xmin_, xmax_] := (minmax = {xmin, xmax}; 
  Table[{10^i, Superscript[10, i]}, 
   {i, Floor[Log10[xmin]], Ceiling[Log10[xmax]]}])

Then,

In[30]:= LogLogPlot[Log[x!], {x, 1, 10^5}, 
  PlotRange -> {{0, 10^5}, {10^-1, 10^6}}, 
  Ticks -> {ticksfun, ticksfun}]; minmax

Out[30]= {-2.30259, 13.8155}

So, ticksfun is being passed a negative number which is evaluating to a Complex number, and the Front End is objecting.

Examining the plot range passed to ticksfun shows that LogLogPlot (and family) are now passing the scaled (logarithmic) range to the ticks function. So, the Log10 in ticksfun are now redundant. Also, the range is in base E, so we need to convert to base 10.

Clear[ticksfun2];
ticksfun2[xmin_, xmax_] := Table[
  {i Log[10], Superscript[10, i]}, 
  {i, Floor[xmin/Log[10]], Ceiling[xmax/Log[10]]}
 ]

enter image description here

(Edit: previous versions failed to take the base change into account. Fixed.)

$\endgroup$
23
  • $\begingroup$ I'd have used Clip[] myself... $\endgroup$ Apr 1, 2016 at 18:15
  • $\begingroup$ Hmm. It should be giving the full range. Give me a sec to make adjustments. $\endgroup$
    – rcollyer
    Apr 1, 2016 at 18:15
  • $\begingroup$ @J.M. I don't think so. Where would I clip it? $\endgroup$
    – rcollyer
    Apr 1, 2016 at 18:19
  • $\begingroup$ @rcollyer Still doesn't work for me $\endgroup$
    – BlacKow
    Apr 1, 2016 at 18:20
  • $\begingroup$ @BlacKow there might be a bug there on early versions of 10. What version are you using? $\endgroup$
    – rcollyer
    Apr 1, 2016 at 18:21

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.