Force scientific notation in tick labels of ListLogLogPlot
I am trying to custom-format tick labels on a ListLogLogPlot. By searching Mathgroup archives, it looks like the usual way to mess with tick labels is to extract t开发者_高级运维hem using AbsoluteOptions
, run a replacement rule with the custom format, and then explicitly feed them to the plotting function with the Ticks->{...}
option. However, the following doesn't work for ListLogLogPlot:
foo = ListLogLogPlot[Range[20]^3, Frame -> True];
ticks=(FrameTicks /. AbsoluteOptions[foo, FrameTicks])
Any ideas on how to deal with this?..
Edit: lots of good answers here! Accepting Mr. Wizard's since it proved to be the most concise way to solve the immediate problem at hand, but I see myself using the other methods suggested in the future.
One can use replacements to mess with the labels directly, bypassing Option
/AbsoluteOptions
:
ListLogLogPlot[Range[20]^3, Frame -> True] /.
(FrameTicks -> x_) :>
(FrameTicks -> (x /. {a_?NumericQ, b_Integer, s___} :>
{a, Superscript[10, Log10@b], s} ))
Thanks to Alexey Popkov this is now improved and less fragile.
Like Sjoerd, I generally prefer to write a function that computes the ticks on the fly:
PowerTicks[label_][min_, max_] := Block[{min10, max10},
min10 = Floor[Log10[min]];
max10 = Ceiling[Log10[max]];
Join[Table[{10^i,
If[label, Superscript[10, i], Spacer[{0, 0}]]}, {i, min10,
max10}],
Flatten[
Table[{k 10^i,
Spacer[{0, 0}], {0.005, 0.`}, {Thickness[0.001`]}}, {i, min10,
max10}, {k, 9}], 1]]
]
ListLogLogPlot[Range[20]^3, Frame -> True,
FrameTicks -> {{PowerTicks[True],
PowerTicks[False]}, {PowerTicks[True], PowerTicks[False]}}]
To complement Brett's answer, look at the CustomTicks
package in LevelScheme. It provides two functions for generating tick marks 'LinTicksand
LogTicks`, and each has a host of formatting options. Currently, it requires you to perform the logarithm yourself, i.e.
Plot[ {Log[10,Cosh[x]], Log[10, Sinh[x]]}, {x, 0, 4}, Frame -> True,
FrameTicks -> { LinTicks, LogTicks, None, None }]
gives
For a list of data, obviously you'd have to use Log[Base, data]
with ListPlot
, but it is workable. I have submitted a patch to Mark Caprio so that the following would do the exact same thing as above
LogPlot[ {Cosh[x], Sinh[x]}, {x, 0, 4}, Frame -> True,
FrameTicks -> { LinTicks, LogTicks, None, None }]
If the patch is accepted the old form of LogTicks
would be accessible by setting the option PlotType
to Linear
, Logarithmic
is default. The advantage of using CustomTicks
is that other bases are easy
and it automatically formats it like you want.
Edit: I'd also like to point out, that CustomTicks
is loadable by itself, separate from the rest of LevelScheme
. And, as it is a small package, there isn't all that much additional overhead.
Looks like a bug to me. Simple calling AbsoluteOptions[foo]
yields error messages. Plain old Options[foo]
works fine, though.
Send an email with this code in it to support@Wolfram.com. They will be able to tell you if there's a known better workaround.
精彩评论