Skip to content Skip to sidebar Skip to footer

Matplotlib's Ticklabel_format(style='plain') Is Ignored Or Fails For Logarithmic Axis But Works For Linear Axis

I would like to use ticklabel_format(style='plain') to suppress scientific notation on a logarithmic axis, but it is either ignored (first plot) or throws an exception (third plot,

Solution 1:

I do not understand why the scientific notation is turned off for the linear axis and why it is either ignored or throws an exception only for the logarithmic axis, but based on this answer I can at least stop the bad behavior.

I still await an answer for why the three cases in the question produce different behavior.

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

x = np.logspace(-3, 3, 19)
y = np.log10(x)

fig = plt.figure()

ax1 = fig.add_subplot(3, 1, 1)
ax1.plot(x, y)
ax1.set_title("style='plain' is ignored", fontsize=16)
ax1.ticklabel_format(style='plain', axis='x')
ax1.set_xscale('log')

ax2 = fig.add_subplot(3, 1, 2)
ax2.plot(x, y)
ax2.set_title("style='plain' works", fontsize=16)
ax2.ticklabel_format(style='plain', axis='x')

if True:
    ax3 = fig.add_subplot(3, 1, 3)
    ax3.plot(x, y)
    ax3.set_title('This now works!', fontsize=16)
    ax3.set_xscale('log')
    formatter = FuncFormatter(lambda y, _: '{:.16g}'.format(y)) # https://stackoverflow.com/a/49306588/3904031
    ax3.xaxis.set_major_formatter(formatter)

plt.show()

it works!


Post a Comment for "Matplotlib's Ticklabel_format(style='plain') Is Ignored Or Fails For Logarithmic Axis But Works For Linear Axis"