For reasons I laid out in an earlier thread, I want to use the undocumented functions Charting`ScaledTicks
and Charting`ScaledFrameTicks
to generate tick specifications for plots.
I'm having a hard time figuring out how to use these functions to generate tick specs for log-scaled plots.
I'm using the following "reference plot" as a test case:
reference = LogLinearPlot[Tanh[x], {x, 1, 100}, ImageSize -> Small,
Ticks -> {Automatic, Automatic}]
My question is:
What values must I give to the transforms
, min
, max
arguments in the assignment
xticks = Charting`ScaledTicks[transforms][min, max];
...such that x-axis ticks in the plot produced by
LogLinearPlot[Tanh[x], {x, 1, 100}, ImageSize -> Small,
Ticks -> {xticks, Automatic}]
...resemble those in the reference
plot?
IMPORTANT: I want to stress that replicating the reference
plot is not the goal here. Rather, the goal here is an explicit list of tick specifications (primarily coordinates and labels), as generated by a call to Charting`ScaledTicks
with suitable parameters. In this question, the reference
plot only serves as a quick way to check that these tick specifications are correct.
Apparently, the reference
plot itself uses Charting`ScaledTicks
under the hood:
First[Ticks /. Options[reference]]
Charting`ScaledTicks[{Log, Exp}]
BTW, the expression Charting`ScaledTicks[{Log, Exp}]
does not evaluate to an explicit list of tick specifications, so it is not the answer to this post's question.
In any case, using Charting`ScaledTicks[{Log, Exp}]
as tick specification, like this
LogLinearPlot[Tanh[x], {x, 1, 100}, ImageSize -> Small,
Ticks -> {Charting`ScaledTicks[{Log, Exp}], Automatic}]
...results in the error
Tick specification must be a list or a function.
What I've tried so far
Here's my first attempt (I eyeballed the endpoints 0.75 and 150 from the reference
plot):
With[{xticks = Charting`ScaledTicks[{Log, Exp}][0.75, 150]},
Column[{
LogLinearPlot[Tanh[x], {x, 1, 100}, ImageSize -> Small,
Ticks -> {xticks, Automatic}],
TableForm @ tickCoords @ xticks (* see end of post for for definition of
tickCoords *)
}]
]
(The table after the plot shows the x-coordinates and labels for the labeled x-axis ticks.)
There are a couple of problems with this, but the most serious one is that the tick labels (10, 1011, ..., 1061) are way off.
(Also, compared to the reference
plot above, this one has much fewer tick marks. That's also something I'd like to correct.)
I figured that maybe the range's endpoints need to be specified in log units, which led to my second attempt:
With[{xticks = Charting`ScaledTicks[{Log, Exp}][Log[0.75], Log[150]]},
Column[{
LogLinearPlot[Tanh[x], {x, 1, 100}, ImageSize -> Small,
Ticks -> {xticks, Automatic}],
TableForm @ tickCoords @ xticks (* see end of post for for definition of
tickCoords *)
}]
]
Now it is the position of the ticks is off, while the labels (1, 5, 10, 50, 100) are at least appropriate to the plot (even if they don't yet match those in the reference
plot).
I tried many other variants, which either triggered errors, or produced no ticks at all.
For the sake of this demonstration, I wrote the following helper function, which just extracts from Charting`ScaledTicks
's output the coordinates of the labeled ticks:
tickCoords[tickSpecs_List] :=
Cases[Take[#, 2] & /@ tickSpecs, Except[{_, _Spacer}]];
I use this helper function to generate the table of x-coordinates and labels shown after each of the plots shown above.