Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I have a conditional format of the values appearing in the axis of a plot ?

I have in mind the number format options available in Excel for plots as described here

I'm mostly interested in how to format numbers as shown in the paragraph "Thousands, Millions, etc." in order to display 100 000 as 100k and 100 000 000 as 100M for example.

Also is it possible to set this format by default in plots ?

share|improve this question
This can be done, with a bit of effort/code, by writing a function f to generate the appropriate ticks specification, and then use Ticks -> {Automatic, f}. But you'll have to determine tick placement, size, etc... on your own (FindDivisions is useful here.) – Brett Champion Mar 24 '12 at 15:26
See my answer here. That gives a general way to format the ticks as you want — you just need to replace the xTicks function with your own logic to convert 10^8 as 100 M, etc. – R. M. Mar 24 '12 at 15:37
up vote 14 down vote accepted

One can also define a KMB number format using NumberForm and its options as follows:

 g[a_] := Switch[a, "3", "K", "6", "M", "9", "B", "12", "T", _, ""]; 
 kmbtForm[num_?NumericQ, digits_?IntegerQ] := 
 StringReplace[#, "." ~~ x : ("K" | "M" | "B" | "T") -> x] &@
 NumberForm[N@#1, #2, 
  ExponentFunction -> (If[0 >= #, 0, 3 Quotient[#, 3]] &), 
  NumberFormat -> (StringJoin[#1, g[#3]] &)] & @@ {num, digits}

Usage examples:

  {kmbtForm[#, 3], kmbtForm[#, 4]} & 
   /@ {-1234, 12.34, 12345.67, 123456.7, 1234567., 123456789.123, 1234567891.} // Grid


enter image description here

For plot ticks, using a variation of Faysal's tick function with this formatting function

 tickfunc[xmin_, xmax_] := 
 Function[tickNumber, {tickNumber, kmbtForm[tickNumber, 3]}] /@ 
 FindDivisions[{xmin, xmax}, 10];


 Plot[1000 x^3, {x, -10, 10}, Ticks -> {Automatic, tickfunc}]


enter image description here

share|improve this answer
This looks quite flexible. +1 – Mr.Wizard Mar 27 '12 at 5:24
Indeed, +1 and accept. – faysou Mar 27 '12 at 7:12
@Mr.Wizard and Faysal, Thank you for the vote and accept. – kglr Mar 27 '12 at 17:57

Here's what I've come up with based on the comment of Brett and the example of Chris that suits what I wanted.





SetOptions[Plot, Ticks -> {Automatic, tickNumberFormat}];

Plot[1000 x^3,{x,-10,10}] 

enter image description here

share|improve this answer

As Brett mentions, for example:

yticks = {#*10^6, ToString[#] <> "m"} & /@ (Range[10]*4);
ListPlot[Prime[Range[25]*10^5], Filling -> Axis, Ticks -> {Automatic, yticks}]

enter image description here

share|improve this answer

Your Answer


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.