Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to LogPlot a function, but I have the trouble in the number format in the ticks.

For example,

LogPlot[Abs[BesselJ[1, x] Sin[x]^2], {x, -10, 10},
   Frame -> True, FrameTicks -> {{Automatic, None}, {None, None}}]

The output is

Plot output

If I use the command

LogPlot[Abs[BesselJ[1, x] Sin[x]^2], {x, -10, 10}, Frame -> True,  
  FrameTicks -> {
    {{#, HoldForm[#]} & /@ {10^0, 10^-1, 10^-2, 10^-3, 10^-4, 10^-5}, None},
    {None, None}
   }
]

I can get

Plot output

Actually, I prefer the all the ticks in the form of 10^n, and none of the commands shown above works.

Is there any simple and clever way to cope with it? I'll be grateful for your reply.

share|improve this question
    
Please see this question and Verbeia's answer. –  kguler May 9 '12 at 9:55
    
See also belisarius' answer to this question: mathematica.stackexchange.com/questions/5276/… –  David Carraher May 9 '12 at 10:00
    
I have fixed it. Thank you for your information –  yulinlinyu May 9 '12 at 10:05
add comment

2 Answers 2

up vote 11 down vote accepted

Perhaps this?

LogPlot[Abs[BesselJ[1, x] Sin[x]^2], {x, -10, 10}, Frame -> True, 
 FrameTicks -> {{{#, Superscript[10, Log10@#]} & /@ ({10^0, 10^-1, 
       10^-2, 10^-3, 10^-4, 10^-5}), None}, {None, None}}]

Mathematica graphics


Here's a completely different approach, manipulating the existing tick labels in the generated graph, and preserving the unlabeled ticks. This seems much cleaner to me than Peter's approach, assuming that it works on version 8 as it does on version 7.

format = Replace[#, {p_, n_?NumericQ} :> {p, Superscript[10, Log10@n]}, {#2}] &;

ticks = MapThread[format, {Options[#, {Ticks, FrameTicks}], {3, 4}}] &;

Use:

p = LogPlot[Abs[BesselJ[1, x] Sin[x]^2], {x, -10, 10}, Frame -> True];

Show[p, ticks[p]]

Mathematica graphics

share|improve this answer
    
Yes, it is a similiar way to mine. LogPlot[Abs[BesselJ[1, x] Sin[x]^2], {x, -10, 10}, Frame -> True, FrameTicks -> {{{10^-#, Superscript[10, -#]} & /@ {0, 1, 2, 3, 4}, None}, {None, None}}] –  yulinlinyu May 9 '12 at 10:06
    
could you also write how to modify the number of digits for the tick labels, for example according to NumberForm[tick, {3, 2}]? thanks –  Valerio Mar 4 '13 at 8:29
    
@Valerio I don't know exactly what you want. Are you also working with LogPlot or just plain Plot? Do you want to change the tick labels on both axes or just the Y axis as done here? If you are around in the next couple of hours we could chat about it. –  Mr.Wizard Mar 4 '13 at 13:02
    
@Mr.Wizard Hi, I was wondering if it is possible to set the number of digits in e.g. the X axis (not necessarily only for LogPlot) –  Valerio Mar 12 '13 at 14:47
1  
@Valerio you can manually specify which ticks to use with the Ticks or FrameTicks option. You can also define a function that generates these ticks, for example: tf = N@FindDivisions[{#, #2}, 20] &; Plot[Sin[x], {x, 0, 2 Pi}, Ticks -> tf] -- this can be done for each axis independently. Be aware with my example that FindDivisions will generate about the number of divisions you specify and not that number exactly. –  Mr.Wizard Mar 13 '13 at 16:02
add comment

If you want the minor ticks too, you can use the following function:

SetAttributes[dtZahl, Listable]
dtZahl[x_] := Block[{n}, If[IntegerQ[n = Rationalize[x]], n, x]]

exponentForm[x_?NumberQ] := 
  Module[{me = MantissaExponent[x], num, exp}, 
   If[MemberQ[{0, 0., 1, 1., -1, -1.}, x], Return[IntegerPart[x]]];
   exp = Superscript["\[CenterDot]10", me[[2]] - 1];
   num = NumberForm[N[me[[1]]]*10 // dtZahl, 3];
   If[me[[1]] == 0.1,(*no mantissa*)num = "";
    exp = Superscript[10, me[[2]] - 1], 
    If[me[[2]] == 1,(*range 0..10*)exp = ""]];
   Row[{num, exp}]];
exponentForm[x_] := x

Options[logTicks] = {TicksFaktor -> 1};
logTicks[von_Integer, bis_Integer, werte_List, subwerte_List, 
  OptionsPattern[]] :=
 Module[{mt, st, ticks, res, tf},
  tf = OptionValue[TicksFaktor];
  mt = {#, exponentForm[N[#]], {0.01, 0}*tf} & /@ 
    Flatten@Table[10^i*werte, {i, von, bis}];
  st = {#, Null, {0.005, 0}*tf} & /@ 
    Flatten@Table[10^i*subwerte, {i, von, bis}];
  Join[mt, st]]

logTicks takes the following Parameters:
von and bis are the lowest and highest exponent. The list werte is the list of labeled ticks in one decade and subwerte the list of unlabeled ticks in one decade.

Example:

GraphicsRow[{
  ticks = logTicks[-4, 1, {1}, {2, 3, 5, 7}];
  LogPlot[Abs[BesselJ[1, x] Sin[x]^2], {x, -10, 10}, Frame -> True, 
   FrameTicks -> {{ticks, None}, {None, None}}],
  ticks = logTicks[-4, 1, {1, 3}, {2, 5, 7}];
  LogPlot[Abs[BesselJ[1, x] Sin[x]^2], {x, -10, 10}, Frame -> True, 
   FrameTicks -> {{ticks, None}, {None, None}}]
  }]

Output: Mathematica graphics

share|improve this answer
    
Thank U for your codes! –  yulinlinyu May 9 '12 at 11:03
    
+1 for flexibility. –  Mr.Wizard May 9 '12 at 21:45
    
+1 very nice function! Don't know why this tick feature is still missing in MMA9... –  Leo Fang Aug 24 '13 at 4:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.