- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图理解这段代码:
def add_inset(ax, rect, *args, **kwargs):
box = ax.get_position()
inax_position = ax.transAxes.transform(rect[0:2])
infig_position = ax.figure.transFigure.inverted().transform(inax_position)
new_rect = list(infig_position) + [box.width * rect[2], box.height * rect[3]]
return fig.add_axes(new_rect, *args, **kwargs)
原码来自this notebook file .
我不明白为什么需要两个坐标变换:
inax_position = ax.transAxes.transform(rect[0:2])
infig_position = ax.figure.transFigure.inverted().transform(inax_position)
最佳答案
add_inset(ax, rect)
方法中,rect
是轴坐标中的矩形。这是有道理的,因为您经常想要指定插图相对于其所在轴的位置。
然而,为了以后能够创建新的轴,轴的位置需要在图形坐标中已知,然后可以将其提供给 fig.add_axes(figurecoordinates)
。所以需要的是从轴坐标到图形坐标的坐标变换。这是在此处分两步执行的:
transAxes
从轴坐标转换为显示坐标。transFigure
的逆运算将显示坐标转换为图形坐标。这两个步骤的过程可以进一步压缩成一个单一的转换
mytrans = ax.transAxes + ax.figure.transFigure.inverted()
infig_position = mytrans.transform(rect[0:2])
阅读 matplotlib transformation tutorial 可能会有兴趣关于转换的工作原理。
以上可能不是放置插图的最明显方法。 Matplotlib 本身提供了一些工具。一个方便的方法是 mpl_toolkits.axes_grid1.inset_locator
。以下是在轴坐标中创建插图时使用其 inset_axes
方法的两种方法。
import matplotlib.pyplot as plt
import mpl_toolkits.axes_grid1.inset_locator as il
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(4,4))
ax1.plot([1,2,3],[2.2,2,3])
# set the inset at upper left (loc=2) with width, height=0.5,0.4
axins = il.inset_axes(ax1, "50%", "40%", loc=2, borderpad=1)
axins.scatter([1,2,3],[3,2,3])
# set the inset at 0.2,0.5, with width, height=0.8,0.4
# in parent axes coordinates
axins2 = il.inset_axes(ax2, "100%", "100%", loc=3, borderpad=0,
bbox_to_anchor=(0.2,0.5,0.7,0.4),bbox_transform=ax2.transAxes,)
axins2.scatter([1,2,3],[3,2,3])
plt.show()
关于python - Matplotlib 坐标变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42932908/
Java 是否存在任何开源或“免费”库,我可以在其中执行从一个空间系统到另一个空间系统的坐标转换? 我找到了 Opengeo http://opengeo.org/但它是一个庞大而全面的图书馆,可以容
我正在尝试使用 java 2d 图形库绘制图形,我认为我已经有了它。我想在坐标系中绘制,其中 0,0 位于面板左边缘的中心。我使用了以下代码,它似乎给了我我需要的结果。 private void do
给出的是一个旋转的矩形,它内接于另一个矩形。 两个矩形都有自己的坐标系。 在内接和旋转的矩形中,有一个点P,其坐标相对于该矩形(红色)。 想要的是该点相对于外部矩形(绿色)的坐标: 编辑:还给出了两个
我试图理解这段代码: def add_inset(ax, rect, *args, **kwargs): box = ax.get_position() inax_position =
在 Python 的 Numpy 模块中,是否有一个函数可以计算数组上的长/高级数学表达式?我听说过 numexp 模块,但不想进一步依赖它。 更好的是,我能否将这些表达式限制为仅表示数组中子数组的第
我有 2 个 3D 体积,它们是彼此的变形版本,我正在尝试估计变形函数。我知道失真是连续且可区分的,但不是简单的仿射变换。我也知道每个点的失真幅度可能不是很大。我有几对 anchor ,我知道它们对应
我是一名优秀的程序员,十分优秀!