1
我想知道圖形分數座標Matplotlib圖的文本註釋的邊界矩形的座標。但是,當我嘗試訪問與註釋關聯的修補程序的「範圍」時,無論文本標籤的大小如何,我都會得到Bbox(x0=-0.33, y0=-0.33, x1=1.33, y1=1.33)
。這些座標似乎與IdentityTransform
相關聯,但不會轉換爲任何有意義的數字分數座標。 如何獲得標註的座標(理想情況下,左下角和右上角)的圖形分數單位?獲取圖形座標Matplotlib註釋標籤的座標
實施例:
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return 10 * np.sin(3*x)**4
x = np.linspace(0, 2*np.pi, 100)
y = f(x)
fig, ax = plt.subplots()
ax.plot(x,y)
xpt = 1.75
ypt = f(xpt)
xy = ax.transData.transform([xpt, ypt])
xy = fig.transFigure.inverted().transform(xy)
xytext = xy + [0.1, -0.1]
rdx, rdy = 0, 1
ann = ax.annotate('A point', xy=xy, xycoords='figure fraction',
xytext=xytext, textcoords='figure fraction',
arrowprops=dict(arrowstyle='->', connectionstyle="arc3",
relpos=(rdx, rdy)),
bbox=dict(fc='gray', edgecolor='k', alpha=0.5),
ha='left', va='top'
)
patch = ann.get_bbox_patch()
print(patch.get_extents())
給出:
[[-0.33 -0.33]
[ 1.33 1.33]]
c = patch.get_transform().transform(patch.get_extents())
print(c)
給出:
[[-211.2 -158.4]
[ 851.2 638.4]]
推測這些是顯示座標,但它們不對應於我想要的屬性的標籤的位置和大小。
謝謝 - 雖然我有一個問題:我只能在'plt.show()'之後重現正確的bbox座標:它不足以調用'fig.canvas.draw()'...是這個後端問題?我在OS X上。 – xnx
因爲我沒有任何關於Mac的經驗,所以我只能說這個方法在Windows下的matplotlib 1.5中有後端'agg','TkAgg'和'Qt4Agg'。 – ImportanceOfBeingErnest
是的 - 這是Mac OS X後端的錯誤。用'agg'很好地工作。謝謝你的幫助。 – xnx