光标下的matplotlib值 [英] matplotlib values under cursor

查看:117
本文介绍了光标下的matplotlib值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib.imshow来获取2D数组的交互式显示.光标下方的x/y坐标显示在窗口的左下方.是否还可以获取光标下的数组的值?

I'm using matplotlib.imshow to get an interactive display of a 2D array. The x/y coordinate under the cursor is displayed at the bottom left of the window. Is it possible to also get the value of the array under the cursor as well?

推荐答案

您只需要重新分配ax.format_coord.请参阅文档中的此示例.

You simply need to re-assign ax.format_coord. See this example from the documentation.

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

(code lifted directly from example)

"""
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()

这篇关于光标下的matplotlib值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆