代码之家  ›  专栏  ›  技术社区  ›  SuperElectric

如何在matplotlib中控制鼠标悬停文本

  •  6
  • SuperElectric  · 技术社区  · 7 年前

    x = 274.99  y = 235.584  [.241, .213, .203]
    

    然而,当我将鼠标悬停在箭图上时,它只显示指针的x坐标和y坐标,而不显示所指向的2D向量的值。有没有办法让向量值显示出来?

    1 回复  |  直到 7 年前
        1
  •  7
  •   ImportanceOfBeingErnest    7 年前

    有时,默认情况下不存在有关颜色值的信息。事实上,我认为当前版本是基于Stackoverflow中关于该特性的一些问题的一些代码。

    想法是更改鼠标悬停在轴上时调用的函数。此函数存储在 ax.format_coord . 因此,一个可能的解决方案是编写自定义函数,根据输入坐标返回所需的输出,例如

    def format_coord(x, y):
        try:
            z = # get value depending on x,y, e.g. via interpolation on grid
                # I can't fill this because the kind of data is unknown here
            return "x: {}, y: {}, z: {}".format(x,y,z)
    
        except:
            return "x: {}, y: {}".format(x,y)
    
    ax.format_coord = format_coord