如何设置小刻度的范围?

How to set the range of minor ticks?

我正在绘制条形图。我绘制的图片如下:

要在顶部和底部轴中添加箭头,我首先将书脊颜色设置为 'none'。

然后我使用 axes.arrow() 函数绘制箭头。

最后,我使用 axes.set_ticks() 函数重置了刻度。

我想保留顶轴的小刻度。但如您所见,左上角的小刻度线超出了箭头的范围。如何删除超出范围的部分?

可以通过set_minor_locator using a FixedLocator设置次刻度。

一个例子:

from matplotlib import pyplot as plt
from matplotlib.ticker import FixedLocator

fig, ax = plt.subplots()
ax.set_xscale('log')
ax.set_xlim(10**6, 1)
ax.set_xticks([10**n for n in range(-1, 5)])
ax.xaxis.set_minor_locator(FixedLocator(
    [k * 10**n for n in range(-1, 5) for k in range(2, 10) if k * 10**n <= 30000]))
ax.xaxis.tick_top()
plt.show()