如何防止matplotlib注释被其他轴剪裁

2024-10-01 17:37:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在matplotlib中有一个带有多个子图(轴)的绘图,我想对轴内的点进行注释。但是,后面的轴会覆盖先前轴的注释(例如子批次(4,4,1)上的注释位于子批次(4,4,2)下)。我已经将注释zorder设置得很高,但是没有用:/

我在注释中使用了Joe Kington'sawesomeDataCursor的修改版本。在

任何帮助都将不胜感激

下面是一个例子: enter image description here


Tags: 版本绘图matplotlib例子joezorderkington轴会
1条回答
网友
1楼 · 发布于 2024-10-01 17:37:57

一种方法是将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()

enter image description here

如果我们只是将文本对象从轴中弹出并添加到图形中,它将位于顶部:

^{pr2}$

enter image description here

您提到了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

我还没有测试最后一点,但它应该可以工作。。。在

相关问题 更多 >

    热门问题