Matplotlib.axes.axes.set_position()
Matplotlib是Python中的一个库,它是NumPy库的数值-数学扩展。Axes包含了大多数图形元素:Axis、Tick、Line2D、Text、Polygon等,并设置坐标系。Axes的实例通过callbacks属性支持回调。
函数:Matplotlib.axes.axes.set_position()
matplotlib库的Axes模块中的Axes.set_position()函数用于设置轴的位置。
Axes.set_position(self)
参数:该方法接受以下参数。
- pos:此参数是图中坐标的新位置。
 - which:该参数用于确定要更改的位置变量。
 返回值:该方法不返回任何值。
下面的例子演示了matplotlib.axes.axes.set_position()函数在matplotlib.axes中的作用:
示例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()
输出:

极客教程