Python 了解Axes.transData如何依赖于xlim和ylim更新

Python 了解Axes.transData如何依赖于xlim和ylim更新,python,matplotlib,transform,Python,Matplotlib,Transform,我一直在学习使用Matplotlib转换。我发现Axes.transData转换不一定包含它需要的信息。以该教程中的示例为例,如果我删除对set_xlim和set_ylim的调用,我们就不会得到期望的结果。缺少一个批注: import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 10, 0.005) y = np.exp(-x/2.) * np.sin(2*np.pi*x) fig, ax = plt.subplo

我一直在学习使用Matplotlib转换。我发现
Axes.transData
转换不一定包含它需要的信息。以该教程中的示例为例,如果我删除对
set_xlim
set_ylim
的调用,我们就不会得到期望的结果。缺少一个批注:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.005)
y = np.exp(-x/2.) * np.sin(2*np.pi*x)

fig, ax = plt.subplots()
ax.plot(x, y)

xdata, ydata = 5, 0
# This computing the transform now, if anything
# (figure size, dpi, axes placement, data limits, scales..)
# changes re-calling transform will get a different value.
xdisplay, ydisplay = ax.transData.transform((xdata, ydata))

bbox = dict(boxstyle="round", fc="0.8")
arrowprops = dict(
    arrowstyle="->",
    connectionstyle="angle,angleA=0,angleB=90,rad=10")

offset = 72
ax.annotate('data = (%.1f, %.1f)' % (xdata, ydata),
            (xdata, ydata), xytext=(-2*offset, offset), textcoords='offset points',
            bbox=bbox, arrowprops=arrowprops)

disp = ax.annotate('display = (%.1f, %.1f)' % (xdisplay, ydisplay),
                   (xdisplay, ydisplay), xytext=(0.5*offset, -offset),
                   xycoords='figure pixels',
                   textcoords='offset points',
                   bbox=bbox, arrowprops=arrowprops)

plt.show()


我不想为我的用例显式地设置
xlim
ylim
。我确实发现,如果在调用
ax.transData.transform
之前调用
ax.getxlim()。这是最好的使用方法,还是有更直接的方法告诉轴进行自我更新,以便
ax.transData
获得所需的信息?还有一个原因是
transData
对象在计算结果之前没有内置某些东西来确保限值是最新的吗?

我仍在寻找解释,但答案是否可以回答您的问题
fig.canvas.draw()
似乎填充了所有坐标系。感谢@wwii,所以查看代码库,似乎
draw
get\uXLIM
调用
ax.\uUnstale\uViewLim()
。我只是试着直接打电话,它确实恢复了我想要的行为。显然,我不会在我的“真实”代码中使用隐藏方法,但它有助于理解发生了什么!事实上,
ax.\u unstale\u viewLim()
调用了
ax.autoscale\u view()
,所以也许我应该直接使用它?我不会直接调用它们-注意它们以下划线开头。我仍然在关注这些碎屑,但我认为为了安全起见,我会一直使用Figure.draw(),因为它应该渲染/缩放所有的艺术家<代码>打印(ax.\u stale\u viewlim\u x,ax.\u stale\u viewlim\u y)
在调用get/set x/ylim或Figure.draw()之前和之后显示这些属性状态的变化。也许现在您已经钻研了一点,您可以重新措辞并提出一个新的/不同的问题,该问题的标题可能会引起像ImportanceOfBeingErnest这样的人的注意。