Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展。轴类包含大多数图形元素:Axis,Tick,Line2D,Text,Polygon等,并设置坐标系。 Axes实例通过callbacks属性支持回调。
matplotlib.axes.Axes.get_aspect()函数
matplotlib库的axiss模块中的Axes.get_aspect()函数用于获取轴缩放的方面,即y-unit与x-unit的比率
用法:Axes.get_aspect(self)
参数:此方法不接受任何参数。
返回值:此方法返回长宽比值。
以下示例说明了matplotlib.axes中的matplotlib.axes.Axes.get_aspect()函数:
范例1:
# ImpleIn Reviewtation of matplotlib function
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_xscale("log")
ax1.set_yscale("log")
ax1.set_adjustable("datalim")
ax1.plot([1, 3, 34, 4, 46, 3, 7, 45, 10],
[1, 9, 27, 8, 29, 84, 78, 19, 48],
"o-", color ="green")
ax1.set_xlim(1e-1, 1e2)
ax1.set_ylim(1, 1e2)
ax2.set_xscale("log")
ax2.set_yscale("log")
ax2.set_adjustable("datalim")
ax2.plot([1, 3, 34, 4, 46, 3, 7, 45, 10],
[1, 9, 27, 8, 29, 84, 78, 19, 48],
"o-", color ="green")
ax2.set_xlim(1e-1, 1e2)
ax2.set_ylim(1, 1e2)
ax2.set_aspect(2)
w = ax1.get_aspect()
w1 = ax2.get_aspect()
ax1.set_title("Axes 1\n Ratio:" +str(w))
ax2.set_title("Axes 2\n Ratio:" +str(w1))
fig.suptitle('matplotlib.axes.Axes.get_aspect() \
function Example\n\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
输出:
范例2:
# ImpleIn Reviewtation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
n_angles = 36
n_radii = 10
min_radius = 2
radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, 2 * np.pi, n_angles,
endpoint = False)
angles = np.repeat(angles[..., np.newaxis],
n_radii, axis = 1)
angles[:, 1::2] += 2 * np.pi / n_angles
x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
triang = tri.Triangulation(x, y)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis = 1),
y[triang.triangles].mean(axis = 1))
< min_radius)
fig, (ax, ax1) = plt.subplots(1, 2)
ax.triplot(triang, 'bo-', lw = 1, color = "green")
ax1.set_aspect('equal')
ax1.triplot(triang, 'bo-', lw = 1, color = "green")
w = ax.get_aspect()
w1 = ax1.get_aspect()
ax.set_title("Axes 1\n Ratio:" +str(w))
ax1.set_title("Axes 2\n Ratio:" +str(w1))
fig.suptitle('matplotlib.axes.Axes.get_aspect() \
function Example\n\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
输出:
注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 Matplotlib.axes.Axes.get_aspect() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。