繁体   English   中英

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

[英]How do I change the data display format for a imshow plot in matplotlib?

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

在此处输入图片说明

我检查了以下回复中提到的所有线程: cursor下的matplotlib值 但是,它们似乎都解决了光标的x,y位置。 我对更改数据值感兴趣。 我在api中找不到回复或相关文档。

我已经尝试了format_coord(x, y)format_cursor_data(data)但它们似乎都不起作用。

谢谢,萨里斯

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

通过修改matplotlib的示例 ,我得到了以下代码:

这将显示x,y和z值,并在z之后加上一个度数

您应该能够轻松地对其进行修改,或者复制相关功能以使其正常工作。

您说您已经尝试过format_coord ,也许您忘记设置功能了? 倒数第二行

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

Emmm,Sarith,我也遇到了问题,但有一点技巧可以帮助我。 我仍然使用此功能:

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

它假装在括号之前添加标签。 是的,这是更改演示文稿形式的简单但非必要的方法,但对我有用。 我希望这也可以使其他人受益。

一线解决方案:

ax.format_coord = lambda x, y: 'x={:.2f}, y={:.2f}, z={:.2f}'.format(x,y,data[int(y + 0.5),int(x + 0.5)])

我有同样的问题(我想摆脱数据并将其发送到tkinter小部件中的其他位置)。 ax.format_coord没有调用ax.format_coord ,您需要更改的是at matplotlib.artist.Artist这对我ax.format_coord

    def format_cursor_data(self,data):            
        return 'new_data'
    matplotlib.artist.Artist.format_cursor_data=format_cursor_data

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM