如何在matplotlib中更改imshow图的数据显示格式?

问题描述 投票:0回答:4

或者更具体地说,如何将右上角的[659]更改为“659度”或类似的东西?

enter image description here

我检查了以下回复中提到的所有主题:matplotlib values under cursor。但是,所有这些似乎都解决了光标的x,y位置。我有兴趣改变数据值。我在api中找不到回复或相关文档。

我试过了format_coord(x, y)format_cursor_data(data),但它们似乎都没有起作用。

谢谢,萨里斯

PS:我的代码在多个模块中,是gui应用程序的一部分。我可以分享相关部分,如果这有助于回答这个问题。

python matplotlib imshow
4个回答
0
投票

通过修改matplotlib中的example我得到了这段代码:

这将显示z后的度数的x,y和z值。

您应该能够轻松修改它,或复制相关功能以使其适用于您。

你说你已经尝试过qazxsw poi,也许你忘了设置这个功能? (倒数第二行)

format_coord

0
投票

嗯,萨里斯,我也面临着这个问题,但是一个小技巧帮助了我。我还是用这个函数:

""" 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, ax = plt.subplots() 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: #get z from your data, given x and y z = X[row, col] #this only leaves two decimal points for readability [x,y,z]=map("{0:.2f}".format,[x,y,z]) #change return string of x,y and z to whatever you want return 'x='+str(x)+', y='+str(y)+', z='+str(z)+" degrees" else: return 'x=%1.4f, y=%1.4f' % (x, y) #Set the function as the one used for display ax.format_coord = format_coord plt.show()

它假装在支架前添加标签。是的,这是一种简单但不是必不可少的方式来改变演示形式,但它对我有用。我希望这也可以使其他人受益。


0
投票

一线解决方案:

your_imshow.format_coord = lambda x, y: 'x={:.5f}, y={:.2f}, amplitude='.format(x,y)

0
投票

我有同样的问题(我想摆脱数据并将其发送到tkinter小部件中的其他地方)。我发现ax.format_coord = lambda x, y: 'x={:.2f}, y={:.2f}, z={:.2f}'.format(x,y,data[int(y + 0.5),int(x + 0.5)]) 没有被叫,你必须改变的那个是ax.format_coord那个对我有用的:

matplotlib.artist.Artist
© www.soinside.com 2019 - 2024. All rights reserved.