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}]
And everything seems right.
But there is a glitch:f[fd_] := Join[
{#, #, {.03, 0}, Directive[Red, Thickness[.01]]} & /@ fd[[1]],
{#, #, {.05, 0}, Direc开发者_运维百科tive[Black, Thin]} & /@ Flatten[fd[[2]]]];
plot[pr_List] :=
Plot[Sin[x], Evaluate@Join[{x}, pr], Ticks -> {f[FindDivisions[pr, {2,5}]]}]
plot[{0, 10}]
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:So, two questions:
- Is this a bug, or am I doing (or understanding) something wrong?
- Any easy way to delete the repeated ticks in a multilevel structure?
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, {{}}]
精彩评论