Python中的 Matplotlib.axes.Axes.autoscale_view()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.autoscale_view()函数
matplotlib 库的 axes 模块中的Axes.autoscale_view()函数用于使用数据限制自动缩放视图限制。
Syntax: Axes.autoscale_view(self, tight=None, scalex=True, scaley=True)
Parameters: This method accepts the following parameters.
- scalex: This parameter is used to whether to autoscale the x axis.
- scaley: This parameter is used to whether to autoscale the y axis.
- tight: This parameter is used to expand the axis limits using the margins.
Return value: This method does not return any value.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.autoscale()函数:
示例 1:
# ImpleIn Reviewtation of matplotlib function
import numpy as np
from basic_units import cm, inch
import matplotlib.pyplot as plt
N = 5
val1 = [150 * cm, 160 * cm, 146 * cm,
172 * cm, 155 * cm]
val2 = [20 * cm, 30 * cm, 32 * cm,
10 * cm, 20 * cm]
fig, ax = plt.subplots()
ind = np.arange(N)
width = 0.35
ax.bar(ind, val1, width, bottom = 0 * cm,
yerr = val2, label ='In Review')
woval1 = (145 * cm, 149 * cm, 172 * cm,
165 * cm, 200 * cm)
woval2 = (30 * cm, 25 * cm, 20 * cm,
31 * cm, 22 * cm)
ax.bar(ind + width, woval1, width,
bottom = 0 * cm, yerr = woval2,
label ='Published')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('Geek1', 'Geek2',
'Geek3', 'Geek4',
'Geek5'))
ax.legend()
ax.set_ylabel("Articles")
ax.autoscale_view()
fig.suptitle('matplotlib.axes.Axes.autoscale_view()\
function Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
from matplotlib import collections, colors, transforms
import numpy as np
nverts = 50
npts = 100
r = np.arange(nverts)
theta = np.linspace(0, 2 * np.pi, nverts)
xx = r * np.sin(theta)
yy = r * np.cos(theta)
spiral = np.column_stack([xx, yy])
rs = np.random.RandomState(19680801)
xyo = rs.randn(npts, 2)
colors = [colors.to_rgba(c)
for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
fig, [ax1, ax2] = plt.subplots(1, 2)
col = collections.RegularPolyCollection(
7, sizes = np.abs(xx) * 10.0, offsets = xyo,
transOffset = ax1.transData)
trans = transforms.Affine2D().scale(fig.dpi / 72.0)
col.set_transform(trans)
ax1.add_collection(col, autolim = True)
col.set_color(colors)
ax1.set_title("Without autoscale_view() function")
col = collections.RegularPolyCollection(
7, sizes = np.abs(xx) * 10.0, offsets = xyo,
transOffset = ax2.transData)
trans = transforms.Affine2D().scale(fig.dpi / 72.0)
col.set_transform(trans)
ax2.add_collection(col, autolim = True)
col.set_color(colors)
ax2.autoscale_view()
ax2.set_title("Using autoscale_view() function")
fig.suptitle('matplotlib.axes.Axes.autoscale_view()\
function Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
输出: