Search code examples
matplotlibplotlogarithm

set ticks with logarithmic scale


It seems that the set_xticks is not working in log scale:

from matplotlib import pyplot as plt
fig1, ax1 = plt.subplots()
ax1.plot([10, 100, 1000], [1,2,3])
ax1.set_xscale('log')
ax1.set_xticks([20, 200, 500])
plt.show()

is it possible?


Solution

  • import matplotlib
    from matplotlib import pyplot as plt
    fig1, ax1 = plt.subplots()
    ax1.plot([10, 100, 1000], [1,2,3])
    ax1.set_xscale('log')
    ax1.set_xticks([20, 200, 500])
    ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
    

    or

    ax1.get_xaxis().get_major_formatter().labelOnlyBase = False
    plt.show()
    

    resulting plot