📌  相关文章
📜  Python中的 Matplotlib.axis.Axis.get_label_text()函数

📅  最后修改于: 2022-05-13 01:54:34.727000             🧑  作者: Mango

Python中的 Matplotlib.axis.Axis.get_label_text()函数

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。它是Python中用于二维数组图的惊人可视化库,用于处理更广泛的 SciPy 堆栈。

Matplotlib.axis.Axis.get_label_text()函数

matplotlib库的axis模块中的axis.get_label_text()函数用于获取标签的文本。

以下示例说明了 matplotlib.axis 中的 matplotlib.axis.Axis.get_label_text()函数:

示例 1:

Python3
# 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.xaxis.set_label_text('X - Label', bbox=box)
ax2.set_xlim(0, 10)
print("Text of the Label :",ax2.xaxis.get_label_text())
    
fig.suptitle("Matplotlib.axis.Axis.get_label_text()\
Function Example", fontsize = 12, fontweight ='bold') 
  
plt.show()


Python3
# 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.yaxis.set_label_text('Default', bbox=box)
ax1.set_ylim(0, 20)
print("Text of Label in Axes 1 :",ax1.yaxis.get_label_text())
    
ax2.set_title('\nLabel is aligned')
ax2.plot(20*np.random.rand(10))
ax2.yaxis.set_label_text('Adjusted', bbox=box)
ax2.yaxis.set_label_position('right')
ax2.set_ylim(0, 20)
print("Text of Label in Axes 2 :",ax2.yaxis.get_label_text())
    
fig.suptitle("Matplotlib.axis.Axis.get_label_text()\
Function Example", fontsize = 12, fontweight ='bold') 
  
plt.show()


输出:

Text of the Label : X - Label

示例 2:

Python3

# 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.yaxis.set_label_text('Default', bbox=box)
ax1.set_ylim(0, 20)
print("Text of Label in Axes 1 :",ax1.yaxis.get_label_text())
    
ax2.set_title('\nLabel is aligned')
ax2.plot(20*np.random.rand(10))
ax2.yaxis.set_label_text('Adjusted', bbox=box)
ax2.yaxis.set_label_position('right')
ax2.set_ylim(0, 20)
print("Text of Label in Axes 2 :",ax2.yaxis.get_label_text())
    
fig.suptitle("Matplotlib.axis.Axis.get_label_text()\
Function Example", fontsize = 12, fontweight ='bold') 
  
plt.show()

输出:

Text of Label in Axes 1 : Default
Text of Label in Axes 2 : Adjusted