Python中的 Matplotlib.ticker.FixedLocator 类
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。
matplotlib.ticker.FixedLocator
matplotlib.ticker.FixedLocator
类是matplotlib.ticker.Locator
类的子类,用于固定刻度位置。如果nbins的值不等于 None,则将对所有可能位置的数组进行二次采样,以保持刻度总数小于或等于nbins + 1。进行二次采样以包括绝对最小的值。例如,如果零包含在可能值的数组中,则它保证选择的刻度。
Syntax: class matplotlib.ticker.FixedLocator(locs, nbins=None)
Parameters:
- locs: It represents the location of the ticks.
- nbins: It represents the number of bins that the data is to be divided into.
类的方法:
- set_params(self, nbins=None):用于设置定位器内的参数。
- tick_value(self, vmin, vmax):返回 vmax 和 vmin 之间的刻度位置。
示例 1:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
np.arange(0, 15, 5)
plt.figure(figsize = [6,4])
x = np.array([1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15])
y = np.array([15, 16, 17, 18,
19, 20, 40, 50,
60, 70, 80, 90,
100, 110, 120])
ax = sns.pointplot(x, y,
color = 'k',
markers = ["."],
scale = 2)
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([1,5,8]))
plt.show()
输出:
示例 2:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t)*np.exp(-t * 0.01)
fig, ax = plt.subplots()
plt.plot(t, s)
ax1 = ax.twiny()
ax1.plot(t, s)
ax1.xaxis.set_ticks_position('bottom')
majors = np.linspace(0, 100, 6)
minors = np.linspace(0, 100, 11)
thirds = np.linspace(0, 100, 101)
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(majors))
ax.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(minors))
ax1.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([]))
ax1.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(thirds))
ax1.tick_params(which ='minor', length = 2)
ax.tick_params(which ='minor', length = 4)
ax.tick_params(which ='major', length = 6)
ax.grid(which ='both', axis ='x', linestyle ='--')
plt.axhline(color ='green')
plt.show()
输出: