获取轴的长宽比 [英] get aspect ratio of axes
问题描述
axes
的纵横比设置为'auto'
时,是否有一种简单可靠的方法来确定axes
的当前纵横比?
Is there an easy and reliable way to determine the current aspect ratio of an axes
when its aspect is set to 'auto'
?
要检查的显而易见的是ax.get_aspect()
,但这仅返回'auto'
.我可以通过ax.set_aspect(aspect)
将其设置为任意常量值,然后通过ax.get_aspect()
返回相同的常量.默认情况下(非常有用),我们有aspect = 'auto'
,在这种情况下,会自动计算和调整纵横比以匹配数据限制和轴大小.
如何获得自动选择的数字长宽比?
The obvious thing to check is ax.get_aspect()
, but that just returns 'auto'
. I can set it to an arbitrary constant value by ax.set_aspect(aspect)
, after which that same constant is returned by ax.get_aspect()
. By default (and very usefully) we have aspect = 'auto'
, in which case the aspect ratio is automatically calculated and adjusted to match the data limits and axes size.
How can I get the numeric aspect ratio that was automatically selected?
为澄清起见,这既不是ax.get_data_ratio()
返回的数据限制的纵横比,也不是返回的图形或子图的显示尺寸的纵横比.通过fig.get_figheight() / fig.get_figwidth()
(对于该图).这有点微妙,因为它取决于显示尺寸和数据限制. (这可能会导致混淆不同的比率,以及我认为使它易于使用很重要的原因.)
To clarify, this is neither the aspect ratio of the data limits returned by ax.get_data_ratio()
, nor the aspect ratio of the display size of the figure or subplot returned by fig.get_figheight() / fig.get_figwidth()
(for the figure). It's a bit subtle, as it depends on both the display size and the data limits. (Which can lead to confusing the different ratios and the reason I find it important to have it easily accessible.)
推荐答案
From the docs, the aspect ratio is the ratio of data-to-display scaling units in the x- and y-axes. i.e., if there are 10 data units per display in the y-direction and 1 data unit per display unit in the x-direction, the ratio would be 1/10. The circle would be 10 times wider than it is tall. This corresponds to the statement that an aspect of num
does the following:
一个圆圈将被拉伸,使得高度为宽度的num倍. Aspect = 1与Aspect =等于"相同.
a circle will be stretched such that the height is num times the width. aspect=1 is the same as aspect=’equal’.
基于您查看过的同一段原始代码( matplotlib.axes._base.adjust_aspect
,从1405行开始),我认为只要您为线性笛卡尔轴只需要该比例,我们就可以提出一个简化的公式.极轴和对数轴会使事情变得复杂,因此我将忽略它们.
Based on the same original piece of code that you looked at (matplotlib.axes._base.adjust_aspect
, starting around line 1405), I think we can come up with a simplified formula as long as you only need this ratio for linear Cartesian axes. Things get complicated with polar and logarithmic axes, so I will ignore them.
要重申该公式:
(x_data_unit / x_display_unit) / (y_data_unit / y_display_unit)
这恰好与
(y_display_unit / x_display_unit) / (y_data_unit / x_data_unit)
最后一个公式只是两个方向上显示尺寸的比率除以x和y极限的比率.请注意,ax.get_data_ratio
不会 在这里适用,因为它返回的是实际数据范围的结果,而不是轴的限制:
This last formulation is just the ratio of the display sizes in the two directions divided by the ratio of the x and y limits. Note that ax.get_data_ratio
does NOT apply here because that returns the results for the actual data bounds, not the axis limits at all:
from operator import sub
def get_aspect(ax):
# Total figure size
figW, figH = ax.get_figure().get_size_inches()
# Axis size on figure
_, _, w, h = ax.get_position().bounds
# Ratio of display units
disp_ratio = (figH * h) / (figW * w)
# Ratio of data units
# Negative over negative because of the order of subtraction
data_ratio = sub(*ax.get_ylim()) / sub(*ax.get_xlim())
return disp_ratio / data_ratio
现在让我们对其进行测试:
Now let's test it:
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
ax.set_aspect('equal')
print('{} == {}'.format(ax.get_aspect(), get_aspect(ax)))
ax.set_aspect(10)
print('{} == {}'.format(ax.get_aspect(), get_aspect(ax)))
这篇关于获取轴的长宽比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!