Python中的 Matplotlib.axes.Axes.get_xaxis_transform()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.get_xaxis_transform()函数
matplotlib 库的 axes 模块中的Axes.get_xaxis_transform()函数用于获取用于绘制 x 轴标签、刻度和网格线的转换。
Syntax: Axes.get_xaxis_transform(self, which=’grid’)
Parameters: This method does not accepts any parameters.
Return value: This method returns the transformation used for drawing x-axis labels, ticks and gridlines.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.get_xaxis_transform()函数:
示例 1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 5.0, 0.1)
s = np.exp(-t) + np.sin(2 * np.pi * t) + 1
nse = np.random.normal(0.0, 0.3, t.shape) * s
fig, vax = plt.subplots()
vax.plot(t, s, 'go-')
vax.vlines(t, [0], s)
vax.vlines([1, 2], 0, 1,
transform = vax.get_xaxis_transform(),
colors ='r')
fig.suptitle('matplotlib.axes.Axes.get_xaxis_transform()\
function Example', fontweight ="bold")
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
x = np.arange(0, 10, 0.005)
y = np.exp(-x / 2.) * np.sin(2 * np.pi * x)
fig, [ax, ax1] = plt.subplots(1, 2)
ax.plot(x, y)
ax.set_title("Without get_xaxis_transform() function")
ax1.plot(x, y, transform = ax1.get_xaxis_transform())
ax1.set_title("With get_xaxis_transform() function")
fig.suptitle('matplotlib.axes.Axes.get_xaxis_transform()\
function Example', fontweight ="bold")
plt.show()
输出: