Python中的 Matplotlib.figure.Figure.get_tightbbox()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 figure 模块提供了顶级 Artist,即 Figure,其中包含所有绘图元素。该模块用于控制所有绘图元素的子图和顶级容器的默认间距。
matplotlib.figure.Figure.get_tightbbox() 方法
matplotlib 库的 get_tightbbox() 方法figure 模块用于获取图形的(紧)边界框,以英寸为单位。
Syntax: get_tightbbox(self, renderer, bbox_extra_artists=None)
Parameters: This method accepts the following parameters.
- renderer : This parameter is the RendererBase instance
renderer that will be used to draw the figures. - bbox_extra_artists : This parameter is the list of artists to include in the tight bounding box.
Returns: This method return BboxBase that contains the bounding box.
下面的示例说明了 matplotlib.figure 中的 matplotlib.figure.Figure.get_tightbbox()函数:
示例 1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
X = np.arange(-10, 10, 0.5)
Y = np.arange(-10, 10, 0.5)
U, V = np.meshgrid(X, Y)
fig, ax = plt.subplots()
ax.quiver(X, Y, U, V)
ax.invert_xaxis()
w = fig.get_tightbbox(fig.canvas.get_renderer(),
bbox_extra_artists = None)
print("Value Return by get_tightbbox():")
print(w)
fig.suptitle('matplotlib.figure.Figure.get_tightbbox()\
function Example', fontweight ="bold")
plt.show()
输出:
Value Return by get_tightbbox():
TransformedBbox(
Bbox(x0=27.65277777777777, y0=29.077777777777776, x1=576.0, y1=422.4),
Affine2D(
[[0.01 0. 0. ]
[0. 0.01 0. ]
[0. 0. 1. ]]))
示例 2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
xx = np.random.rand(16, 30)
fig, ax = plt.subplots()
m = ax.pcolor(xx)
m.set_zorder(-20)
w = fig.get_tightbbox(fig.canvas.get_renderer(),
bbox_extra_artists = None)
print("Value Return by get_tightbbox():")
print(w)
fig.suptitle('matplotlib.figure.Figure.get_tightbbox()\
function Example', fontweight ="bold")
plt.show()
输出:
Value Return by get_tightbbox():
TransformedBbox(
Bbox(x0=52.52777777777777, y0=29.077777777777776, x1=584.875, y1=427.9),
Affine2D(
[[0.01 0. 0. ]
[0. 0.01 0. ]
[0. 0. 1. ]]))