如何减少matplotlib对数图上的主刻度线间距

时间:2018-01-23 13:57:20

标签: python matplotlib

我想将主刻度标签间距更改为0.1(基数0.1)的间隔,并在下面的代码中以0.01间距设置次要刻度(不带标签)(输出显示在1中。

x = np.array([1, 5, 10, 32])
y = np.array([0.34, 0.27, 0.25, 0.21])
yerr = np.array([0.02, 0.019, 0.019, 0.016])

fig, ax = plt.subplots()
res = ax.errorbar(x, y, yerr=yerr, fmt='.', ls='')

ax.set_xscale('log')
ax.set_yscale('log')

我已经尝试过了

import matplotlib.ticker as mtick
ax.yaxis.set_major_locator(mtick.LogLocator(base=0.01))

然而没有任何变化,这让我觉得基数不能低于10。

2 个答案:

答案 0 :(得分:0)

编辑:好的,这背后的推理是错误的(& I misundertood the question)

这会在刻度线之间增加等距空间,绝对不是你要求的。

plt.grid("on")  # Just to see the spacing more clearly
ax.yaxis.set_major_locator(mtick.LogLocator(base=10**(1.0/1)))
ax.yaxis.set_minor_locator(mtick.LogLocator(base=10**(1.0/10)))

(因为10是您正在使用的对数刻度的基础,而分数是您想要多少(或更少)的行。

我不明白使用对数刻度时'base'变量中的小数值是如何工作的。

好吧,因为你正在使用对数值......'间距'不应该是1或更小。 (如果你想减少间距,则使用1),只需使用小于10(但大于1)的值。

实际上,使用以下行:

plt.grid("on")  # Just to see the spacing more clearly
ax.yaxis.set_major_locator(mtick.LogLocator(base=10**(1/10)))

你得到的间距是以前的十分之一。

使用对数刻度时,全部是如何计算垂直轴。

答案 1 :(得分:0)

解决方案:感谢ImportanceOfBeingErnest提供建议。我的解决方案是添加:

import matplotlib.ticker as mtick
ax.yaxis.set_major_locator(mtick.LogLocator(base=10, subs=range(10)))
ax.yaxis.set_minor_locator(mtick.LogLocator(base=10, subs=range(100)))
plt.setp(ax.get_yminorticklabels(), visible=False);