以对数刻度控制刻度线间距

时间:2014-12-02 20:17:58

标签: python numpy matplotlib

申请时:

ax.set_yscale('log')

到matplotlib中的一个轴,它为10的每个倍数创建一个勾号。有时,这可能会很多,例如见下面的截图:

enter image description here

相反,我希望勾选100的每一个倍数,或1000的每个倍数,同时保留对数缩放。

我怎样才能在matplotlib中做到这一点?

1 个答案:

答案 0 :(得分:3)

只需使用matplotlib.ticker.LogLocator

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LogLocator
x = np.linspace(0, 10, 10)
y = 2**x
f = plt.figure()
ax = f.add_subplot(111)
plt.yscale('log')
ax.yaxis.set_major_locator(LogLocator(base=100))
ax.plot(x, y)
plt.show()

enter image description here 如果您愿意,可以使用次要定位器进行相同操作,或者以您喜欢的任何其他方式进行调整。

相关问题