24

How can I stop the y-axis displaying a logarithmic notation label on the y-axis?

I'm happy with the logarithmic scale, but want to display the absolute values, e.g. [500, 1500, 4500, 11000, 110000] on the Y-axis. I don't want to explicitly label each tick as the labels may change in the future (I've tried out the different formatters but haven't successfully gotten them to work). Sample code below.

Thanks,

-collern2

import matplotlib.pyplot as plt
import numpy as np

a = np.array([500, 1500, 4500, 11000, 110000])
b = np.array([10, 20, 30, 40, 50])

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_yscale('log')

plt.plot(b, a)
plt.grid(True)
plt.show()
user809167
  • 241
  • 1
  • 2
  • 3

2 Answers2

33

If I understand correctly,

ax.set_yscale('log')

any of

ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%d'))
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, pos: str(int(round(x)))))

should work. '%d' will have problems if the tick labels locations wind up being at places like 4.99, but you get the idea.

Note that you may need to do the same with the minor formatter, set_minor_formatter, depending on the limits of the axes.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
DSM
  • 342,061
  • 65
  • 592
  • 494
  • +1 for also giving an example based on a `lambda`. This pretty much opens the door for every possible value-to-string mapping. Imho this answer should be accepted. – bluenote10 Feb 07 '14 at 19:01
3

Use ticker.FormatStrFormatter

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

a = np.array([500, 1500, 4500, 11000, 110000])
b = np.array([10, 20, 30, 40, 50])

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_yscale('symlog')

ax.yaxis.set_major_formatter(ticker.FormatStrFormatter("%d"))

plt.plot(b, a)
plt.grid(True)

plt.show()
Joe Cat
  • 179
  • 1
  • 8