首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止matplotlib批注被其他轴裁剪

问如何防止matplotlib批注被其他轴裁剪
EN

Stack Overflow用户
提问于 2012-12-12 10:26:14
回答 1查看 4.2K关注 0票数 6

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

我已经使用了Joe Kington's a DataCursor的修改版本来进行注释。

任何帮助都将不胜感激

下面是一个例子:

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-12 11:38:28

一种方法是将annotate创建的文本弹出轴,并将其添加到图中。这样,它将显示在所有子图的顶部。

作为您遇到的问题的一个快速示例:

代码语言:javascript
复制
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()

​

​

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

代码语言:javascript
复制
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方法:

代码语言:javascript
复制
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

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

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13831824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档