Python中的 Matplotlib.ticker.LogLocator 类
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用。
matplotlib.ticker.LogLocator
matplotlib.ticker.LogLocator
类用于确定日志轴的刻度位置。在此类中,刻度线放置在以下位置:subs[j]*base**i。
Syntax: class matplotlib.ticker.LogLocator(base=10.0, subs=(1.0, ), numdecs=4, numticks=None)
Parameter:
- subs: It is an optional parameter which is either None, or string or a sequence of floats. It defaults to (1.0, ). It provides the multiples of integer powers of the base at which is used to place ticks. Only at integer powers of the base the default places ticks. The auto and all are the only accepted string values here. The ticks are placed exactly between integer powers with ‘auto’ whereas with “all’ the integers power are accepted. Here None value is equivalent to ‘auto’.
类的方法:
- base(self, base):此方法用于设置对数刻度的基数。
- nonsingular(self, vmin, vmax):用于根据需要扩大范围以避免奇异性。
- set_params(self, base=None, subs=None, numdecs=None, numticks=None):用于设置刻度范围内的参数。
- tick_values(self, vmin, vmax):此方法返回在 vmin 和 vmax 范围之间定位的刻度值。
- subs(self, subs):用于设置每个base**i*subs[j]的对数缩放的次要刻度。
- view_limit(self, vmin, vmax):此方法在智能选择 vie 限制时派上用场。
示例 1:
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, LogLocator
x = [1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
y = [0.32, 0.30, 0.28, 0.26,
0.24, 0.22, 0.20, 0.18,
0.16, 0.14, 0.12, 0.10]
fig = plt.figure()
ax1 = fig.add_subplot(111)
x_major = MultipleLocator(4)
x_minor = MultipleLocator(1)
ax1.xaxis.set_major_locator(x_major)
ax1.xaxis.set_minor_locator(x_minor)
ax1.set_yscale("log")
y_major = LogLocator(base = 10)
y_minor = LogLocator(base = 10, subs =[1.1, 1.2, 1.3])
ax1.yaxis.set_major_locator(y_major)
ax1.yaxis.set_minor_locator(y_minor)
ax1.plot(x, y)
plt.show()
输出:
示例 2:
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()
输出: