【问题标题】:Add information to matplotlib Navigation toolbar/status bar?向 matplotlib 导航工具栏/状态栏添加信息?
【发布时间】:2013-04-08 09:45:54
【问题描述】:

我正在用 matplotlib 绘制一个二维数组。在窗口的右下角显示光标的 x 和 y 坐标。如何在此状态栏中添加有关光标下方数据的信息,例如,而不是“x=439.501 y=317.744”,它会显示“x,y:[440,318] 数据:100”?我能否以某种方式获得此导航工具栏并编写我自己的要显示的消息?

我已经设法为“button_press_event”添加了我自己的事件处理程序,以便在终端窗口上打印数据值,但这种方法只需要大量的鼠标点击并淹没交互式会话。

【问题讨论】:

标签: python matplotlib


【解决方案1】:

您只需重新分配ax.format_coord,即用于绘制该标签的回调。

请参阅文档中的this example,以及 In a matplotlib figure window (with imshow), how can I remove, hide, or redefine the displayed position of the mouse?matplotlib values under cursor

(直接从示例中提取的代码)

"""
Show how to modify the coordinate formatter to report the image "z"
value of the nearest pixel given x and y
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

X = 10*np.random.rand(5,3)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(X, cmap=cm.jet, interpolation='nearest')

numrows, numcols = X.shape
def format_coord(x, y):
    col = int(x+0.5)
    row = int(y+0.5)
    if col>=0 and col<numcols and row>=0 and row<numrows:
        z = X[row,col]
        return 'x=%1.4f, y=%1.4f, z=%1.4f'%(x, y, z)
    else:
        return 'x=%1.4f, y=%1.4f'%(x, y)

ax.format_coord = format_coord
plt.show()

【讨论】:

  • 在谷歌搜索之后,我仍然没有找到这个!感谢您的解决方案!
  • @northaholic 如果这解决了你的问题,你能接受吗(左边的大灰色复选标记)
猜你喜欢
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
相关资源
最近更新 更多