Python中的 Matplotlib.axes.Axes.get_position()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.get_position()函数
matplotlib 库的 axes 模块中的Axes.set_position()函数用于获取轴矩形的副本作为 Bbox..
Syntax: Axes.get_position(self, original=False)
Parameters:
- original : This parameter is used to return the original position if true. Otherwise return the active position.
Return value: This method returns a copy of the axes rectangle as a Bbox.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.get_position()函数:
示例 1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
y = [2, 4, 6, 14, 15, 16, 17, 16, 18, 20]
y2 = [10, 11, 12, 13, 8, 10, 12, 14, 18, 19]
fig, ax1 = plt.subplots()
ax1.plot(x, y, "go-", label ='Line 1', )
ax1.plot(x, y2, "o-", label ='Line 2')
chartBox = ax1.get_position()
x, y, w, h = chartBox.x0, chartBox.y0, chartBox.width, chartBox.height
ax1.text(0, 20, "Bbox - xmin : "+str(x),
fontweight ="bold")
ax1.text(0, 19, "Bbox - ymin : "+str(round(y, 2)),
fontweight ="bold")
ax1.text(0, 18, "Bbox - width : "+str(w),
fontweight ="bold")
ax1.text(0, 17, "Bbox - height : "+str(h),
fontweight ="bold")
fig.suptitle('matplotlib.axes.Axes.get_position()\
function Example\n', fontweight ="bold")
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
Z = np.random.rand(6, 30)
fig, ax1 = plt.subplots()
ax1.pcolor(Z)
chartBox = ax1.get_position()
x, y, x1, y1 = chartBox.x0, chartBox.y0, chartBox.x1, chartBox.y1
ax1.text(4, 6.35, "Bbox - xmin : "+str(x),
fontweight ="bold")
ax1.text(19, 6.35, "Bbox - ymin : "+str(round(y, 2)),
fontweight ="bold")
ax1.text(4, 6.15, "Bbox - xmax : "+str(x1),
fontweight ="bold")
ax1.text(19, 6.15, "Bbox - ymax : "+str(y1),
fontweight ="bold")
fig.suptitle('matplotlib.axes.Axes.get_position()\
function Example\n', fontweight ="bold")
plt.show()
输出: