Python中的 Matplotlib.axes.Axes.autoscale()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.autoscale()函数
matplotlib 库的 axes 模块中的Axes.autoscale()函数用于将轴视图自动缩放到数据(切换)。
Syntax: Axes.autoscale(self, enable=True, axis=’both’, tight=None)
Parameters: This method accepts the following parameters.
- enable : If this parameter is True (default) turns autoscaling on, False turns it off.
- axis: This parameter is used to which axis to be operate on. {‘both’, ‘x’, ‘y’}
- tight: This parameter is forwarded to autoscale_view.
Return value: This method does not return any value.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.autoscale()函数:
示例 1:
# Implementation of matplotlib function
import numpy as np
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import matplotlib.pyplot as plt
vertices = []
codes = []
codes = [Path.MOVETO] + [Path.LINETO]*3 + [Path.CLOSEPOLY]
vertices = [(1, 1), (1, 2), (2, 2),
(2, 1), (0, 0)]
codes += [Path.MOVETO] + [Path.LINETO]*2 + [Path.CLOSEPOLY]
vertices += [(4, 4), (5, 5), (5, 4),
(0, 0)]
vertices = np.array(vertices, float)
path = Path(vertices, codes)
pathpatch = PathPatch(path, facecolor ='None',
edgecolor ='green')
fig, ax = plt.subplots()
ax.add_patch(pathpatch)
ax.autoscale()
fig.suptitle('matplotlib.axes.Axes.autoscale() \
function Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import EllipseCollection
x = np.arange(10)
y = np.arange(15)
X, Y = np.meshgrid(x, y)
XY = np.column_stack((X.ravel(), Y.ravel()))
fig, ax = plt.subplots()
ec = EllipseCollection(10, 10, 5, units ='y',
offsets = XY * 0.5,
transOffset = ax.transData,
cmap ="inferno")
ec.set_array((X * Y).ravel())
ax.add_collection(ec)
ax.autoscale_view()
ax.set_xlabel('X')
ax.set_ylabel('y')
cbar = plt.colorbar(ec)
cbar.set_label('X + Y')
fig.suptitle('matplotlib.axes.Axes.autoscale() function \
Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
输出: