Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindDivisions[ ] not working as stated

FindDivisions[ ] was added in Mma v7, and seems a nice way to get flexible ticks for plots. See for example this question and its answers.

Usage example:

f[fd_] := Join[
   {#, #, {.07, 0}, Directive[Black, Thickness[.01]]} & /@ fd[[1]],
   {#, #, {.05, 0}, Directive[Black, Thin]}           & /@ Flatten[fd[[2]]]];
plot[pr_List] :=  
     Plot[Sin[x], Evaluate@Join[{x}, pr], Ticks -> {f[FindDivisions[pr, {2,5}]]}]

plot[{0, 10}]

enter image description here

And everything seems right.
But there is a glitch:

f[fd_] := Join[
   {#, #, {.03, 0}, Directive[Red, Thickness[.01]]} & /@  fd[[1]], 
   {#, #, {.05, 0}, Directive[Black, Thin]}         & /@  Flatten[fd[[2]]]];
plot[pr_List] :=
  Plot[Sin[x], Evaluate@Join[{x}, pr], Ticks -> {f[FindDivisions[pr, {2,5}]]}]
plot[{0, 10}]

enter image description here

As you can see, the red and black ticks are superimposed. That is because

FindDivisions[{0, 2}, {2, 4}]
(*
-> {{0, 1, 2}, {{0, 1/4, 1/2, 3/4, 1}, {1, 5/4, 3/2, 7/4, 2}}}
*)

and you can see that the numbers in the first list (the main ticks) are repeated in the second list.
However, the FindDivisions[] documentation states:

enter image description here

So, two questions:

  1. Is this a bug, or am I doing (or understanding) something wrong?
  2. Any easy way to delete the repeated ticks in a multilevel structure?
like image 236
Dr. belisarius Avatar asked Jun 16 '11 14:06

Dr. belisarius


1 Answers

It is a bug, probably in implementation, although having the duplicated values might be useful at times. (It is certainly useful for constructing the different levels of divisions.)

For ticks, I'd probably use code like:

{major, minor} = FindDivisions[{0, 2}, {2, 4}];
minor = Complement[Flatten[minor], major];

to flatten the hierarchy and remove duplicates.


Generalized, for more levels than just two:

divs = Flatten /@ FindDivisions[{0, 2}, {2, 4, 2}];
Complement[#2, #1] & @@@ Partition[divs, 2, 1, -1, {{}}]
like image 97
Brett Champion Avatar answered Oct 15 '22 00:10

Brett Champion