Python中的 Matplotlib.axes.Axes.set_position()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.set_position()函数
matplotlib 库的 axes 模块中的Axes.set_position()函数用于设置坐标轴位置。
Syntax: Axes.set_position(self)
Parameters: This method accepts the following parameters.
- pos : This parameter is the new position of the in Figure coordinates.
- which : This parameter is used to determines which position variables to change.
Return value: This method does not return any value.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.set_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, ax = plt.subplots()
ax.plot(x, y, "go-", label ='Line 1', )
ax.plot(x, y2, "o-", label ='Line 2')
chartBox = ax.get_position()
ax.set_position([chartBox.x0, chartBox.y0,
chartBox.width,
chartBox.height * 0.6])
ax.legend(loc ='upper center',
bbox_to_anchor =(0.5, 1.45),
shadow = True, ncol = 1)
fig.suptitle('matplotlib.axes.Axes.set_position()\
function Example', 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, (ax, ax1) = plt.subplots(1, 2)
ax.pcolor(Z)
ax1.pcolor(Z)
chartBox = ax1.get_position()
ax1.set_position([chartBox.x0,
chartBox.y0,
chartBox.width,
chartBox.height * 0.6])
ax.set_title("Original Window")
ax1.set_title("Modified Window")
fig.suptitle('matplotlib.axes.Axes.set_position()\
function Example', fontweight ="bold")
plt.show()
输出: