jason
jason

Reputation: 4439

Matplotlib: setting the Y-axis to none scientific notation AFTER changing the Y-axis to log scale

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()

Upvotes: 1

Views: 3483

Answers (1)

JohanC
JohanC

Reputation: 80319

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

Upvotes: 2

Related Questions