我有这个代码:from pylab import *
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
import numpy as np
import matplotlib.pylab as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ann = AnchoredText('If you zoom in or out, i stay here\nbut can you update this text?',
prop=dict(size=8), frameon=True,
loc=2,
)
ann.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(ann)
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = (Z1 - Z2) * 10
plt.contourf(X, Y, Z)
plt.show()
有没有一种方法可以锚定文本,使其始终可见,但也可以像文本对象一样使用set_text方法来更新包含的文本吗?
这里我使用了AnchoredText,它被完美地锚定了,但是我找不到一个方法来更改它包含的文本。
在AnchoredTextMatplotlib文档中,我找不到这样一个可用的方法。
如果不能用AnchoredText来完成,可以用一个简单的文本对象来完成吗?在
编辑
我接受了来自David Zwicker的解决方案,下面是一个可供任何需要的人使用的示例:
^{pr2}$