Python中的 Matplotlib.figure.Figure.align_xlabels()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 figure 模块提供了顶级 Artist,即 Figure,其中包含所有绘图元素。该模块用于控制所有绘图元素的子图和顶级容器的默认间距。
matplotlib.figure.Figure.align_xlabels()函数
如果标签对齐是自动完成的,matplotlib 库的 align_xlabels() 方法图模块用于将子图的 xlabels 与相同的子图行或列对齐。
Syntax: align_xlabels(self, axs=None)
Parameters: This accept the following parameters that are described below:
- axs : This parameter is the list of Axes to align the xlabels.
Returns: This method does not return any value.
下面的示例说明了 matplotlib.figure 中的 matplotlib.figure.Figure.align_xlabels()函数:
示例 1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
for i in range(2):
ax = fig.add_subplot(gs[1, i])
ax.set_ylabel('Y label')
ax.set_xlabel('X label')
if i == 0:
for tick in ax.get_xticklabels():
tick.set_rotation(45)
fig.align_xlabels()
fig.suptitle('matplotlib.figure.Figure.align_xlabels() \
function Example\n\n', fontweight ="bold")
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
fig = plt.figure(tight_layout = True)
gs = gridspec.GridSpec(2, 2)
ax = fig.add_subplot(gs[0, :])
ax.plot(np.arange(0, 1e6, 1000))
ax.set_ylabel('YLabel0')
ax.set_xlabel('XLabel0')
for i in range(2):
ax = fig.add_subplot(gs[1, i])
ax.plot(np.arange(1., 0., -0.1) * 2000.,
np.arange(1., 0., -0.1))
ax.set_ylabel('YLabel1 % d' % i)
ax.set_xlabel('XLabel1 % d' % i)
if i == 0:
for tick in ax.get_xticklabels():
tick.set_rotation(55)
fig.align_xlabels()
fig.suptitle('matplotlib.figure.Figure.align_xlabels() \
function Example\n\n', fontweight ="bold")
plt.show()
输出: