Consider the following example:
import matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlim([2e-2, 2e-1])
ax.set_ylim([2e+1, 2e+3])
ax.plot([0.02, 0.1, 0.2], [20, 1000, 2000])
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.ScalarFormatter())
plt.show()
which gives
Now, I wish to selectively remove/change tick-labels from the x-axis, insofar to keep only ['0.02', '0.10', '0.20']
, while keeping the tick-markers. How do I do this?
Edit
The strategy that I originally wanted to follow seems to suffer from a bug. I considered this
fig.canvas.draw()
labels = [item.get_text() for item in ax.get_xticklabels()]
# ...
ax.set_xticklabels(labels)
But on my mac it only prints
>>> print(labels)
['', '', '', '', '']