Matplotlib.axes.axes.autoscale()
Matplotlib是Python中的一个库,它是NumPy库的数值-数学扩展。Axes包含了大多数图形元素:Axis、Tick、Line2D、Text、Polygon等,并设置坐标系。Axes的实例通过callbacks属性支持回调。
matplotlib.axes.axes.autoscale()函数
matplotlib库的Axes模块中的Axes.autoscale()函数用于将axis视图自动缩放到数据(toggle)。
Axes.autoscale(self, enable=True, axis= ‘ both ‘, tight=None)
参数:该方法接受以下参数。
- enable:如果该参数为True(默认值)则开启自动缩放,False则关闭自动缩放。
- axis:该参数用于指定要操作的轴。{‘both’,‘x’,‘y’}
- tight:转发给autoscale_view。
返回值:该方法不返回任何值。
下面的例子演示了matplotlib.axes.axes.autoscale()函数在matplotlib.axes中的作用:
示例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()
输出: