Matplotlib.axis.axis.set_label_position()
matplotlib库的Axis模块中的Axis.set_label_position()函数用于设置标签位置。
语法:Axis.set_label_position(self, position)
参数:该方法接受以下参数。
- position:这个参数是标签{‘ top ‘, ‘ bottom ‘, ‘ left ‘, ‘ right ‘}的位置。
返回值:该方法不返回任何值。
下面的例子演示了matplotlib.axis.axis.set_label_position()函数在matplotlib.axis中的作用:
示例1
# Implementation of matplotlib function
from matplotlib.axis import Axis
import numpy as np
import matplotlib.pyplot as plt
fig, ax2 = plt.subplots(sharex = True)
fig.subplots_adjust(left=0.2, wspace=0.6)
box = dict(facecolor='green', pad=5, alpha=0.2)
ax2.plot(20*np.random.rand(10))
ax2.set_xlabel('X - Label', bbox=box)
ax2.set_xlim(0, 10)
ax2.xaxis.set_label_position("top")
fig.suptitle("Matplotlib.axis.Axis.set_label_position()\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
输出:
示例2
# Implementation of matplotlib function
from matplotlib.axis import Axis
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(2, 1, sharex = True)
fig.subplots_adjust(left=0.2, wspace=0.6)
box = dict(facecolor='green', pad=5, alpha=0.2)
np.random.seed(19680801)
ax1.plot(2*np.random.rand(10))
ax1.set_title('Label is not aligned')
ax1.set_ylabel('Default', bbox=box)
ax1.set_ylim(0, 20)
ax2.set_title('\nLabel is aligned')
ax2.plot(20*np.random.rand(10))
ax2.set_ylabel('Adjusted', bbox=box)
ax2.set_ylim(0, 20)
ax2.yaxis.set_label_position("right")
fig.suptitle("Matplotlib.axis.Axis.set_label_position()\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
输出: