Python中的 Matplotlib.figure.Figure.init_layoutbox()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 figure 模块提供了顶级 Artist,即 Figure,其中包含所有绘图元素。该模块用于控制所有绘图元素的子图和顶级容器的默认间距。
matplotlib.figure.Figure.init_layoutbox() 方法
matplotlib 库的init_layoutbox() 方法图模块用于初始化 layoutbox 以供在 constrained_layout 中使用。
Syntax: init_layoutbox(self)
Parameters: This method does not accept any parameters.
Returns: This method does not returns any value.
下面的示例说明了 matplotlib.figure 中的 matplotlib.figure.Figure.init_layoutbox()函数:
示例 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.init_layoutbox()
fig.suptitle("""matplotlib.figure.Figure.init_layoutbox()
function Example\n\n""", fontweight ="bold")
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
fig.subplots_adjust(top = 0.8)
ax1 = fig.add_subplot(211)
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
line, = ax1.plot(t, s, color ='green', lw = 2)
np.random.seed(19680801)
ax2 = fig.add_axes([0.15, 0.1, 0.7, 0.3])
n, bins, patches = ax2.hist(np.random.randn(1000), 50,
facecolor ='yellow',
edgecolor ='yellow')
fig.init_layoutbox()
fig.suptitle("""matplotlib.figure.Figure.init_layoutbox()
function Example\n\n""", fontweight ="bold")
plt.show()
输出: