python - axes_divider 主机轴的 inset_axis 的纵横比(相对于大小和位置)

标签 python matplotlib

在 matplotlib 中,我可以在矩形主轴中创建方形 inset_axes:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

base_ax = plt.axes()

loc="upper left"
size = 20

bbox = base_ax.get_window_extent()
ratio = bbox.width / bbox.height
inset_right_upper = inset_axes(
    base_ax, width=str(size / ratio) + '%', height=str(size) + '%', loc=loc)

enter image description here

当我添加普通轴时,我可以轻松地以编程方式创建插入轴,例如正方形长宽比和具体位置:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

base_ax = plt.subplot2grid((4, 4), (0, 0), rowspan=3, colspan=3)
ax_right = plt.subplot2grid((4, 4), (0, 3), rowspan=3, colspan=1)
ax_bottom = plt.subplot2grid((4, 4), (3, 0), colspan=3)

loc="upper left"
size = 20

bbox = ax_right.get_window_extent()
ratio = bbox.width / bbox.height
inset_right_upper = inset_axes(
    ax_right, width=str(size / ratio) + '%', height=str(size) + '%', loc=loc)

bbox = ax_bottom.get_window_extent()
ratio = bbox.width / bbox.height
inset_bottom_upper = inset_axes(
    ax_bottom, width=str(size / ratio) + '%', height=str(size) + '%', loc=loc)

enter image description here

或者,我可以使用 axis_divider 模式将轴与 anchor 轴相邻,然后将插图添加到相邻的轴:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

base_ax = plt.axes()
divider = make_axes_locatable(base_ax)
ax_right = divider.append_axes("right", size="20%", pad="1%")
ax_bottom = divider.append_axes("bottom", size="20%", pad="1%")

loc="upper left"
size = 20

bbox = ax_right.get_window_extent()
ratio = bbox.width / bbox.height
inset_right_upper = inset_axes(
    ax_right, width=str(size / ratio) + '%', height=str(size) + '%', loc=loc)

bbox = ax_bottom.get_window_extent()
ratio = bbox.width / bbox.height
inset_bottom_upper = inset_axes(
    ax_bottom, width=str(size / ratio) + '%', height=str(size) + '%', loc=loc)

但是,插图会被压扁;即使我指定了方形纵横比(相对于主机轴),它们最终也会被压缩:

enter image description here

我可以强制小轴的纵横比相等:

inset_right_upper.set_aspect('equal')
inset_bottom_upper.set_aspect('equal')

但这会使它们变小并移动它们(这里向中心移动)。

enter image description here

当插入轴插入到axes_divider轴时,插入轴的行为似乎有所不同,并且一些如何为普通轴设置具有特定长宽比、大小和位置的插入的模式不再起作用。是否有用于创建例如的编程解决方案非方形 axes_divider.append_axes 轴的方形插入轴 – 同时保持所需的(边缘拥抱)位置和大小,就像它适用于普通轴一样(参见第二个示例)?

最佳答案

只要图形尚未绘制,分隔线轴还不知道它们的大小。因此,它们的 bbox 与父 bbox 一样大。因此,inset_axes 是根据主轴尺寸计算的。

这实际上是一个或多或少的一般规则:如果您想对艺术家使用.get_window_extent(),您需要确保在获取其范围之前它至少被绘制过一次。

要绘制图形,

plt.gcf().canvas.draw()

完整代码:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

base_ax = plt.subplot(111)

divider = make_axes_locatable(base_ax)
ax_right = divider.append_axes("right", size="20%", pad="1%")
ax_bottom = divider.append_axes("bottom", size="20%", pad="1%")

loc="upper left"
size = 20

plt.gcf().canvas.draw()
bbox = ax_right.get_window_extent()
ratio = bbox.width / bbox.height
inset_right_upper = inset_axes(
    ax_right, width=str(size / ratio) + '%', height=str(size) + '%', loc=loc)


bbox = ax_bottom.get_window_extent()
ratio = bbox.width / bbox.height
inset_bottom_upper = inset_axes(
    ax_bottom, width=str(size / ratio) + '%', height=str(size) + '%', loc=loc)

plt.show()

制作

enter image description here

关于python - axes_divider 主机轴的 inset_axis 的纵横比(相对于大小和位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49500637/

相关文章:

python - 没有 Yield 的上下文管理器

python - 为什么 x[ :, 0] = x[0] 不能用于单个行向量?

python - 在 Matplotlib 中删除楔形极坐标图周围的空间

python - 从 Axes(或Figure)获取 QuadMesh 对象

python - 将 pandas 日期转换为周数

python - PCA 计算中的复杂特征值

python - 对于数据帧列中的每个值,我想需要在另一列( Pandas )上创建值

python - 获取 histogram2d 的每个 bin 中的频率

python - 在 matplotlib.collections.PathCollection 中设置特定补丁的属性

python - 如何在 Matplotlib 中切割长标签