0

I have a simple graph with the object axs for axes. Before I change the Y-axis to log-scale the format is just regular numbers.

After I change the Y-axis to a log scale using: axs.set_yscale('log') ... Then try to change the numbers back with

axs.set_yticklabels(['{:,}'.format(int(x)) for x in axs.get_yticks().tolist()])

It doesn't work... the labels still remain in scientific notation.

I just want the regular numbers back.

I'm using:

fig = plt.figure(figsize=(30, 15))
axs = fig.add_subplot(1, 1, 1)
axs.plot()
jason
  • 3,811
  • 18
  • 92
  • 147
  • 1
    See [here](https://stackoverflow.com/questions/21920233/matplotlib-log-scale-tick-label-number-formatting/33213196). `from matplotlib.ticker import ScalarFormatter`; `axs.yaxis.set_major_formatter(ScalarFormatter())` – JohanC Mar 14 '20 at 18:21
  • Thanks. I tried that. I get the same result. – jason Mar 14 '20 at 18:29
  • yeah,. i will post one later – jason Mar 14 '20 at 18:34

1 Answers1

1

As explained, here you can set a ScalarFormatter to leave out scientific notation. .set_scientific(False) would be needed to also suppress the scientific notation for large numbers.

You might need axs.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) if you're dealing with negative powers.

from matplotlib import pyplot as plt
from matplotlib.ticker import ScalarFormatter

fig = plt.figure(figsize=(30, 15))
axs = fig.add_subplot(1, 1, 1)
axs.plot()
axs.set_ylim(100000, 100000000)
axs.set_yscale('log')
formatter = ScalarFormatter()
formatter.set_scientific(False)
axs.yaxis.set_major_formatter(formatter)
plt.show()

resulting plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • This works for powers of 10, but not for something like 50... for 50 it will show me 5 x 10^1 and 60 -> 6 x 10^1... however at 100, it shows 100 and not 1 x 10^2... same for 400... 4 x 10^2 – jason Mar 14 '20 at 20:40
  • 1
    It would help a lot if you would explain your explicit example in the question. In this case, you have both major and minor ticks. To also change the minor ticks, you could also set `axs.yaxis.set_minor_formatter(formatter)` – JohanC Mar 14 '20 at 21:10
  • That did it. the minor. was going to post an example... but busy stocking up for the virus... sorry. – jason Mar 14 '20 at 21:32