简体   繁体   English

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

[英]Controlling tick spacing in log-scale

When I apply: 申请时:

ax.set_yscale('log')

to an axes in matplotlib, it creates a tick for every multiple of 10. Sometimes, this can bee to much, eg see screenshot below: 对于matplotlib中的一个轴,它会为10的每个倍数创建一个刻度。有时,这可能会很多,例如,请参见下面的屏幕截图:

在此输入图像描述

Instead, I would like to have a tick, say, every multiple of 100 , or every multiple of 1000 , while preserving a logarithmic scaling. 相反,我希望有一个刻度,比方说,每100倍数,或1000每个倍数,同时保留对数缩放。

How can I do that in matplotlib? 我怎么能在matplotlib中做到这一点?

Just use matplotlib.ticker.LogLocator 只需使用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()

在此输入图像描述 And do the same with minor locator if you wish, or adjust it any other way you like. 如果您愿意,可以使用次要定位器进行相同操作,或者以您喜欢的任何其他方式进行调整。

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

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