Search code examples
matplotlibplotloglog

Custom xticks labels in loglog plot


A simple example is as follows:

import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
N = 1000
x = np.linspace(1, 5, N)
y = npr.randint(1e16, size = N) / 1e16
y = np.sort(y)

fig, ax = plt.subplots()
ax.loglog(x, y, '.')
ax.grid(True, 'both')

enter image description here

Where I want to replace the xticks. So far everything I tried, failed to work:

ax.set_xticks([2, 3, 4], ['a', 'b', 'c'])
ax.xaxis.set_ticks_position('none') 
ax.set_xticks([])

None of the above showed any effect. My goal is to replace the ticks with custom defined ticks (strings or integers). So instead of 2 x 10⁰ it should only be 2. Similar for other xticks.


Solution

  • Probably this is what you're after:

    import numpy as np
    import matplotlib.pyplot as plt
    N = 1000
    x = np.linspace(1, 5, N)
    y = np.random.rand(N)
    y = np.sort(y)
    
    fig, ax = plt.subplots()
    ax.loglog(x, y, '.')
    ax.grid(True, 'both')
    
    ax.set_xticks([2, 3, 4])
    ax.set_xticklabels(['a', 'b', 'c'])
    ax.minorticks_off()
    
    plt.show()
    

    enter image description here