gpt4 book ai didi

光标下的 matplotlib 值

转载 作者:行者123 更新时间:2023-12-03 10:28:22 29 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Interactive pixel information of an image in Python?

(5 个回答)


6年前关闭。




我正在使用 matplotlib.imshow获得二维数组的交互式显示。光标下的 x/y 坐标显示在窗口的左下方。是否也可以获取光标下数组的值?

最佳答案

您只需要重新分配 ax.format_coord .见 this 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 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14754931/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com