使用matplotlib进行交互式图像绘图

时间:2011-09-30 19:07:56

标签: matplotlib

我正在从Matlab过渡到NumPy / matplotlib。 matplotlib中似乎缺少的一个功能是交互式绘图。 Zooming and panning很有用,但对我而言,一个常见的用例是:

我使用imshow()绘制灰度图像(Matlab和matplotlib都做得很好)。在出现的图中,我想精确定位一个像素(它的x和y坐标)并得到它的值。

这在Matlab图中很容易做到,但有没有办法在matplotlib中做到这一点?

This似乎很接近,但似乎并不适用于图像。

2 个答案:

答案 0 :(得分:2)

自定义事件处理程序是您需要的。这并不难,但也不是“它只是起作用”。

This question似乎非常接近你所追求的。如果您需要任何澄清,我很乐意添加更多信息。

答案 1 :(得分:1)

我相信你已经设法做到了这一点。略微(!)修改link,我编写了下面的代码,它在绘图区域内点击了x和y坐标。

from pylab import * 
import sys
from numpy import *
from matplotlib import pyplot

class Test:

  def __init__(self, x, y):
    self.x = x
    self.y = y

  def __call__(self,event):
    if event.inaxes:
      print("Inside drawing area!")
      print("x: ", event.x)
      print("y: ", event.y)
    else:
      print("Outside drawing area!")

if __name__ == '__main__':     
  x = range(10)
  y = range(10)      
  fig = pyplot.figure("Test Interactive")
  pyplot.scatter(x,y)
  test = Test(x,y)
  connect('button_press_event',test)     
  pyplot.show()

此外,这将使您更容易理解交互式绘图的基础知识,而不是烹饪书link中提供的基础知识。

P.S。:该程序将提供精确的像素位置。该位置的值应该为我们提供相应像素的灰度值。

以下内容也可以提供帮助: http://matplotlib.sourceforge.net/users/image_tutorial.html

相关问题