3

If we use scientific notation for plotting data as you know usually 1e{+b} would be noted on top of axes. I would like to put 10^{b} instead of the notations of 1e{+b}. Is there any way to accomplish this?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
rhtica
  • 545
  • 1
  • 5
  • 5

1 Answers1

3

Yes, you could use matplotlib.ticker.FuncFormatter, try this:

import matplotlib.pyplot as plt 
import matplotlib.ticker as mtick
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
xs = np.logspace(1,10,10)

ax.plot(xs,range(10))
ax.set_xscale('log')

#the next line is generally wrong, it works just as an example
#ax.xaxis.set_major_formatter(mtick.FuncFormatter(lambda value,pos: ("$10^{%d}$" % pos) ))

#this way change the ticks formats
#import math
#ax.xaxis.set_major_formatter(mtick.FuncFormatter(lambda v,_: ("$10^{%d}$" % math.log(v,10)) ))

#maybe this is what you want
ax.xaxis.set_major_formatter(mtick.ScalarFormatter(useMathText=True))

plt.show()

The above example will render as latex but if you prefer to show the '^' symbol just use "10^{%d}" % pos on your function.

Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68
  • There is also http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.FormatStrFormatter which does exactly this without needing to define a lambda (and there will be a version for new-style format strings in 1.4) – tacaswell Feb 24 '14 at 16:56
  • @tcaswell Yes, but with `FormatStrFormatter` I got the value of the tick, no? I've used the position as a more easy example (I don't have to make any calculation), this `FormatStrFormatter("$10^{%d}$")` will not work (at least on my version of matplotlib [1.2.1]). – Alvaro Fuentes Feb 24 '14 at 17:15
  • yeah, your are correct. The format string formatter is only useful when you can beat what you want into a format string. Now that I read your answer more carefully, it is very wrong. You should not trust the `pos` value and do `10^{%d}"% log(val))`. If the user changes the view limits the ticks will not change. (it won't let my take my +1 back.....) – tacaswell Feb 24 '14 at 17:19
  • Yes I know that's wrong as a general solution but I just wanted to show a simple example (thats why I've names the params with long names not just `v,p`), first I was typing the math calculations with value but finally I just post the example with `pos`. I will update my answer. – Alvaro Fuentes Feb 24 '14 at 17:24
  • It maybe a simple example, but it is a _dangerous_ simple example. – tacaswell Feb 24 '14 at 17:25
  • Thank you so much xndrme and tcaswell. I tried xndrme's suggestion but it was different from what I want. I'm sorry for the misleading question. I want to put 10^{} at the top of the axes not as tick mark labels. I don't want to use log tick marks either. I would apologize if you already answered my question...I mean if I could achieve with a small modification of your the first answer. – rhtica Feb 25 '14 at 00:50
  • 1
    Please see: http://stackoverflow.com/questions/11577665/change-x-axes-scale-in-matplotlib here they placed scientific notations nicely but the formats was the 1e{} style. I just want to change this to 10^{} style. – rhtica Feb 25 '14 at 01:03
  • Do you want the '^' symbol to be rendered or you accept latex rendering? I've updated my answer, hope it helps now :) – Alvaro Fuentes Feb 25 '14 at 13:38
  • Exactly!Thanks a lot xndrme. – rhtica Feb 26 '14 at 00:35