简体   繁体   English

如何减少Matplotlib对数图上的主要刻度间距

[英]How to reduce major tick spacing on a matplotlib logarithmic plot

I would like to change the major tick label spacing to intervals of 0.1 (base 0.1) and have minor ticks (without labels) at 0.01 spacing for the code below (output shown in 1 . 我想将下面的代码的主要刻度标签间距更改为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')

I've tried to do 我试着做

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

however nothing changes, which makes me think the base can not be less than 10. 但是没有任何变化,这使我认为基数不能小于10。

EDIT: OK, the reasoning behind this is wrong ( & I misundertood the question) 编辑:好的,这背后的原因是错误的(&我误解了问题)

This would add equidistant space between the ticks, definitely not you were asking. 这将在刻度之间添加等距的空间,绝对不是您要的。

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)))

(asuming 10 is the base of the log scale you're using and the fraction is how many more (or less) lines you want. (假设10是您使用的对数刻度的底数,而小数则是所需的行数(或更少)。

I did not understand how the decimal values in the 'base' variable work when using logarithmic scale. 使用对数标度时,我不明白“基数”变量中的十进制值如何工作。


Well, since you're working in logarithmic values ... that 'spacing' shouldn't be 1 or less. 好吧,因为您使用的是对数值...“间距”不应为1或更小。 (using 1 you get an error) if you want to reduce spacing, just use a value between less than 10 (but more than 1). (使用1会导致错误),如果要减小间距,请使用小于10(但大于1)之间的值。

In fact, using the line: 实际上,使用以下行:

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

you get a tenth of a spacing than before. 您得到的间距比以前大十分之一。

It's all in how the vertical axes is computed when using the logarithmic scale. 这就是使用对数刻度时如何计算垂直轴的全部内容。

SOLUTION: Thanks ImportanceOfBeingErnest for the advice. 解决方案:感谢ImportanceOfBeingErnest的建议。 My solution is to add this: 我的解决方案是添加以下内容:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM