在matplotlib图窗口(使用imshow)中,如何删除,隐藏或重新定义鼠标的显示位置? [英] In a matplotlib figure window (with imshow), how can I remove, hide, or redefine the displayed position of the mouse?
问题描述
我正在使用带有matplotlib的ipython,我以这种方式显示图像:
I am using ipython with matplotlib, and I show images in this way:
(启动:ipython --pylab)
(started up with: ipython --pylab)
figure()
im = zeros([256,256]) #just a stand-in for my real images
imshow(im)
现在,当我将光标移到图像上时,我看到鼠标的位置显示在图窗口的左下角。显示的数字是x =列号,y =行号。这是非常以情节为导向而不是以图像为导向。我可以修改显示的数字吗?
Now, as I move the cursor over the image, I see the location of the mouse displayed in the lower left corner of the figure window. The numbers displayed are x = column number, y = row number. This is very plot-oriented rather than image-oriented. Can I modify the numbers displayed?
- 我的第一选择是显示x =行号*标量,y =列号*标量
- 我的第二个选择是显示x =行号,y =列号
- 我的第三个选择是不显示鼠标位置的数字在所有
我可以做这些事吗?我甚至不确定将鼠标悬停测试显示小部件称为什么。谢谢!
Can I do any of these things? I'm not even sure what to call that little mouse-over test display widget. Thanks!
推荐答案
您可以通过简单地重新分配 format_coord,在每个轴上完成此操作
,如示例。 Axes
对象的
You can do this quite simply on a per axis basis by simply re-assigning format_coord
of the Axes
object, as shown in the examples.
format_coord
是任何带2个参数的函数(x ,y)并返回一个字符串(然后显示在图上。
format_coord
is any function which takes 2 arguments (x,y) and returns a string (which is then displayed on the figure.
如果你想没有显示,只需这样做:
If you want to have no display simply do:
ax.format_coord = lambda x, y: ''
如果你只需要行和列(没有检查)
If you want just the row and column (with out checking)
scale_val = 1
ax.format_coord = lambda x, y: 'r=%d,c=%d' % (scale_val * int(x + .5),
scale_val * int(y + .5))
如果你想在你做的每个 iimage上做这个,只需定义包装函数
If you want to do this on every iimage you make, simply define the wrapper function
def imshow(img, scale_val=1, ax=None, *args, **kwargs):
if ax is None:
ax = plt.gca()
im = ax.imshow(img, *args, **kwargs)
ax.format_coord = lambda x, y: 'r=%d,c=%d' % (scale_val * int(x + .5),
scale_val * int(y + .5))
ax.figure.canvas.draw()
return im
经过大量测试我认为应该或多或少地替代 plt .imshow
which with out much testing I think should more-or-less be drop-in replacement for plt.imshow
这篇关于在matplotlib图窗口(使用imshow)中,如何删除,隐藏或重新定义鼠标的显示位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!