7
$\begingroup$

I have looked into previous questions about forcing scientific notation frame ticks and even I do the same thing my plot still has the wrong ticks.

Instead of 1/1000 I want to have $10^{-3}$.

enter image description here

RegionPlot[{Subscript[C, WW] + Subscript[C, BB] < 0}, {Subscript[C, 
  WW], -1.1*10^(-3), 1.1*10^(-3)}, {Subscript[C, BB], -1.1*10^(-3), 
  1.1*10^(-3)}, PlotPoints -> 50, 
 PlotRange -> {{-1.1* 10^(-3), 1.1*10^(-3)}, {-1.1*10^(-3), 
    1.1*10^(-3)}}, 
 FrameTicks -> {{#, ScientificForm@#} & /@ 
    Range[-10^-3, 10^-3, 10^-3], {#, ScientificForm@#} & /@ 
    Range[-10^-3, 10^-3, 10^-3]}, ImageSize -> 450]
$\endgroup$

3 Answers 3

5
$\begingroup$

You can fix this by writing you expression for the FrameTicks option is a much simpler way. To get the ScientificForm to format your limits correctly, you should note the use of 10.^-3 in place of 10^-3.

Plot[x, {x, -1.1*^-3, 1.1*^-3},
  Frame -> True,
  FrameTicks -> ConstantArray[{#, ScientificForm @ #} & /@ 10.^-3 {-1, 0, 1}, 2], 
  ImageSize -> 450]

plot

Update

The above plot doesn't print .001 as 1.*10^(-3) because, by default, numbers that can show all their significant digits when printed at normal output width are not printed in exponent form. However, we can suppress the default behavior. Like so:

tickF = 
  {#, 
   ScientificForm[#, 
     NumberFormat -> (Function[{m, b, e}, If [e == "", m, Row[{m, "×", 10^e}]]])]} &;

Plot[x, {x, -1.1*^-3, 1.1*^-3}, 
  Frame -> True, 
  FrameTicks -> ConstantArray[tickF /@ (10.^-3 {-1, 0, 1}), 2], 
  ImageSize -> 450]

plot

$\endgroup$
2
$\begingroup$

Need .s after each 10 to make ScientificForm working.

RegionPlot[{Subscript[C, WW] + Subscript[C, BB] < 0}, {Subscript[C, 
   WW], -1.1*10^(-3), 1.1*10^(-3)}, {Subscript[C, BB], -1.1*10^(-3), 
  1.1*10^(-3)},
 PlotPoints -> 50, 
 PlotRange -> {{-1.1*10^(-3), 1.1*10^(-3)}, {-1.1*10^(-3), 
    1.1*10^(-3)}}, 
 FrameTicks -> {{#, ScientificForm@#} & /@ 
    Range[-10.^-3, 10^-3, 10.^-3], {#, ScientificForm@#} & /@ 
    Range[-10.^-3, 10^-3, 10.^-3]},
 ImageSize -> 450]

enter image description here

or

RegionPlot[{Subscript[C, WW] + Subscript[C, BB] < 0}, {Subscript[C, 
   WW], -1.1*10^(-3), 1.1*10^(-3)}, {Subscript[C, BB], -1.1*10^(-3), 
  1.1*10^(-3)},
 PlotPoints -> 50, 
 PlotRange -> {{-1.1*10^(-3), 1.1*10^(-3)}, {-1.1*10^(-3), 
    1.1*10^(-3)}}, 
 FrameTicks -> {{#, 
      If[# == 0, 0, 
         ScientificForm[#, 
          NumberFormat -> (Superscript[#2, #3] &)]] &@#} & /@ 
    Range[-10.^-3, 10^-3, 
     10.^-3], {#, 
      If[# == 0, 0, 
         ScientificForm[#, 
          NumberFormat -> (Superscript[#2, #3] &)]] &@#} & /@ 
    Range[-10.^-3, 10^-3, 10.^-3]},
 ImageSize -> 450]

enter image description here

$\endgroup$
2
$\begingroup$

A post processing way would be more automatic.

Define below functions

ClearAll[changeTicks, changeTicksToScientificForm];

changeTicks[plot_, tickStringTransformFunc_, 
   opts : OptionsPattern[]] := Module[{frameTicks, ticks},
   frameTicks = Quiet@AbsoluteOptions[plot, FrameTicks];
   ticks = Quiet@AbsoluteOptions[plot, Ticks];
   Show[plot
      /. (FrameTicks -> _) -> 
       Quiet@First[
         frameTicks /. 
          x_String :> If[x === "", "", tickStringTransformFunc@x]]
     /. (Ticks -> _) -> 
      Quiet@First[
        ticks /. 
         x_String :> If[x === "", "", tickStringTransformFunc@x]], 
    opts]
   ];

changeTicksToScientificForm[plot_, opts : OptionsPattern[]] := 
 Module[{},
  changeTicks[plot, ScientificForm@N@ToExpression@# &, opts]
  ]

Now

p = RegionPlot[{Subscript[C, WW] + Subscript[C, BB] < 0}, {Subscript[
     C, WW], -1.1*10^(-3), 
    1.1*10^(-3)}, {Subscript[C, BB], -1.1*10^(-3), 1.1*10^(-3)}, 
   PlotPoints -> 50, 
   PlotRange -> {{-1.1*10^(-3), 1.1*10^(-3)}, {-1.1*10^(-3), 
      1.1*10^(-3)}}, ImageSize -> 450];
changeTicksToScientificForm@p

gives

enter image description here

changeTicksToScientificForm suites for many other cases. Just apply it to the plots after they are generated. For examples

changeTicksToScientificForm@Plot[x^2, {x, 0, 1000}]

gives

enter image description here

changeTicksToScientificForm@
 DensityPlot[x y, {x, -4, 4}, {y, -100, 100}]

gives

enter image description here

changeTicksToScientificForm@Plot3D[x y^2 , {x, -1, 1}, {y, -1, 100}]

gives

enter image description here

If you do not want to see small number becomes scientific notation, you can use for example ScientificNotationThreshold -> {-3, 3} option for ScientificForm in changeTicksToScientificForm, then you get

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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