我在matplotlib中有一个带有多个子图(轴)的图,我想注解轴中的点。但是,后续轴会覆盖前一个轴的注释(例如,子图(4,4,1)上的注释位于子图(4,4,2)下)。我已经将注释zorder设置得很好很高,但没有用:/
我已经使用了Joe Kington's a DataCursor的修改版本来进行注释。
任何帮助都将不胜感激
下面是一个例子:

发布于 2012-12-12 11:38:28
一种方法是将annotate创建的文本弹出轴,并将其添加到图中。这样,它将显示在所有子图的顶部。
作为您遇到的问题的一个快速示例:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=5, ncols=5)
plt.setp(axes.flat, xticks=[], yticks=[], zorder=0)
ax = axes[0,0]
ax.annotate('Testing this out and seeing what happens', xy=(0.5, 0.5),
xytext=(1.1, .5), textcoords='axes fraction', zorder=100)
plt.show()

如果我们只是将文本对象弹出轴,并将其添加到图中,它将位于顶部:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=5, ncols=5)
plt.setp(axes.flat, xticks=[], yticks=[], zorder=0)
ax = axes[0,0]
ax.annotate('Testing this out and seeing what happens', xy=(0.5, 0.5),
xytext=(1.1, .5), textcoords='axes fraction', zorder=100)
fig.texts.append(ax.texts.pop())
plt.show()

您提到了DataCursor代码片段,并且您想要更改annotate方法:
def annotate(self, ax):
"""Draws and hides the annotation box for the given axis "ax"."""
annotation = ax.annotate(self.template, xy=(0, 0), ha='right',
xytext=self.offsets, textcoords='offset points', va='bottom',
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')
)
# Put the annotation in the figure instead of the axes so that it will be on
# top of other subplots.
ax.figure.texts.append(ax.texts.pop())
annotation.set_visible(False)
return annotation我还没有测试最后一点,但它应该可以工作...
https://stackoverflow.com/questions/13831824
复制相似问题