Python中的 Matplotlib.axis.Axis.get_majorticklabels()函数
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。它是Python中用于二维数组图的惊人可视化库,用于处理更广泛的 SciPy 堆栈。
Matplotlib.axis.Axis.get_majorticklabels()函数
matplotlib 库的轴模块中的Axis.get_majorticklabels()函数用于获取主要刻度标签的 Text 实例列表。
Syntax: Axis.get_majorticklabels(self)
Parameters: This method does not accepts any parameter.
Return value: This method returns the list of Text instances for the major ticklabels.
下面的示例说明了 matplotlib.axis 中的 matplotlib.axis.Axis.get_majorticklabels()函数:
示例 1:
Python3
# Implementation of matplotlib function
import numpy as np
from matplotlib.axis import Axis
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
np.random.seed(19680801)
fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20))
formatter = ticker.FormatStrFormatter('%1.2f')
Axis.set_major_formatter(ax.yaxis, formatter)
print("Value of get_major_ticks() :")
for i in ax.xaxis.get_majorticklabels():
print(i)
plt.title("Matplotlib.axis.Axis.get_majorticklabels()\n\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
Python3
# Implementation of matplotlib function
from matplotlib.axis import Axis
from matplotlib.artist import Artist
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
n = 100000
x = np.random.standard_normal(n)
y = 2 * np.random.standard_normal(n)
z =[1, 2, 3, 4]
xmin = x.min()
xmax = x.max()
ymin = y.min()
ymax = y.max()
fig, ax = plt.subplots()
hb = ax.hexbin(x, y,
gridsize = 50,
bins ='log',
cmap ='BuGn')
ax.set(xlim =(xmin, xmax), ylim =(ymin, ymax))
ax.xaxis.set_ticklabels(["A","B","C","D","E","F","G","H","I","J","K"])
print("Value of get_majorticklabels() :")
for i in ax.xaxis.get_majorticklabels():
print(i)
plt.title("Matplotlib.axis.Axis.get_majorticklabels()\n\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
输出:
Value of get_majorticklabels() :
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
示例 2:
Python3
# Implementation of matplotlib function
from matplotlib.axis import Axis
from matplotlib.artist import Artist
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
n = 100000
x = np.random.standard_normal(n)
y = 2 * np.random.standard_normal(n)
z =[1, 2, 3, 4]
xmin = x.min()
xmax = x.max()
ymin = y.min()
ymax = y.max()
fig, ax = plt.subplots()
hb = ax.hexbin(x, y,
gridsize = 50,
bins ='log',
cmap ='BuGn')
ax.set(xlim =(xmin, xmax), ylim =(ymin, ymax))
ax.xaxis.set_ticklabels(["A","B","C","D","E","F","G","H","I","J","K"])
print("Value of get_majorticklabels() :")
for i in ax.xaxis.get_majorticklabels():
print(i)
plt.title("Matplotlib.axis.Axis.get_majorticklabels()\n\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
输出:
Value of get_majorticklabels() :
Text(0, 0, 'A')
Text(0, 0, 'B')
Text(0, 0, 'C')
Text(0, 0, 'D')
Text(0, 0, 'E')
Text(0, 0, 'F')
Text(0, 0, 'G')
Text(0, 0, 'H')
Text(0, 0, 'I')
Text(0, 0, 'J')
Text(0, 0, 'K')