python - 如何在可见窗口中保留注释?

标签 python python-3.x matplotlib

如何在 Matplotlib 的窗口中保留注释?基本上,我想解决这个问题,以便黄色框可见:

enter image description here

由这个 mcve 生成​​的:

from matplotlib import pyplot

point = pyplot.scatter(0, 0)
s = '''This is some really long text to go into the annotation that is way
too long to fit inside the figure window.'''

annotation = pyplot.annotate(
    s=s, xy=(0, 0), xytext=(-10, 10), textcoords='offset points',
    ha='right', va='bottom', bbox=dict(boxstyle='round,pad=0.5', fc='yellow')
)

pyplot.show()

我打算将注释作为鼠标悬停,但没有必要实现该代码来显示问题(我已经想出了如何做到这一点)。但是,也许有一种方法可以使用 tkinter 向其添加工具提示,这可能会完全解决我的问题。

也许有一种方法可以检测到注释在窗口之外。如果有这样的方法,这个伪代码会做我想做的事:

if annotation.is_not_within(window):
    draw_annotation_to_go_the_other_way(annotation)

不幸的是,我还没有找到这样的方法。

此外,也许有一个内置方法可以自动将注释保持在边界内。我也一直找不到这样的方法。

如何将注释保持在图形窗口的范围内?

最佳答案

既然你说过如果你知道注释是否在框架内就可以做到这一点,那么这里有一种方法可以做到这一点:

def in_frame(annotation):
    pyplot.show(block=False) # we need this to get good data on the next line
    bbox = annotation.get_bbox_patch()
    uleft_coord = (0, bbox.get_height())
    pnt = bbox.get_data_transform().transform_point(uleft_coord)
    return pyplot.gcf().get_window_extent().contains(*pnt)

想法是我们从标注的bbox(边界框)中抓取一个点,然后询问图形(框架)的bbox是否包含该点。这只检查左上角的点,但我们可以通过检查更多的角来变得更好。对于您的图像,这将返回 False。以下是一些返回 true 的示例:

enter image description here enter image description here

还有一个返回 false 的:

enter image description here

让我们更详细地解释一下这个函数,这样你就可以使用它了:

pyplot.show(block=False)             # display the figure, so that the annotation bbox
                                     # will return the actual box. Otherwise it says the
                                     # bbox has 1 pixel width and height.
bbox = annotation.get_bbox_patch()
uleft_coord = (0, bbox.get_height())

# kind of scary, so let's break this down:
pnt = bbox.get_data_transform().transform_point(uleft_coord)
      bbox.get_data_transform()      # this gives us a transform that can
                                     # convert a pixel from the coordinates
                                     # of the bbox to coordinates that the
                                     # figure uses.
pnt = (...).transform_point(uleft_coord) # this converts the uleft_coord into
                                         # the coordinate system of the figure
# This is that all together:
pnt = bbox.get_data_transform().transform_point(uleft_coord)

# here's another of those scary lines:
return pyplot.gcf().get_window_extent().contains(*pnt)
       pyplot.gcf()                     # this calls "get current figure"
       pyplot.gcf().get_window_extent() # and now let's get its bbox
       (       ...     ).contains(*pnt) # and ask it if it contains pnt
return pyplot.gcf().get_window_extent().contains(*pnt)

链接:

关于python - 如何在可见窗口中保留注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768102/

相关文章:

python - 如何在 matplotlib 中绘制数据和函数之间的差异

python - 如何在散点图中可视化非线性关系

python - 使用python pty伪终端进程发送命令并退出

python - 列表2> sum13编码 bat 问题: 'int' object is not iterable

python - 了解 Pytorch 网格示例

python - 如何在列表循环中找到特定位置的最小值和最大值?

python - 使用lora接收树莓派上的杂乱数据

python - Matplotlib:缩放后找出 xlim 和 ylim

python - 使用 BeautifulSoup 抓取网站以下载其上的所有文档会抛出 IOError

python - 将多个 .ui 文件导入 main.py 文件