【问题标题】:Interactive image plotting with matplotlib使用 matplotlib 进行交互式图像绘图
【发布时间】:2011-09-30 19:07:55
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

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

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

    【讨论】:

    • 这看起来很有希望。我会试一试,让你知道。当我搜索 SO 时,我没有找到那个问题。出于我的目的,我不需要插值,因此从图像数组中检索值对我来说是最好的方法。我很欣赏 matplotlib 的可扩展性,但内置这样的东西肯定会很好。
    • 抱歉耽搁了。是的,您对这个问题的回答有一个非常漂亮的事件处理程序。它可以很好地满足我的需要,尽管在鼠标悬停而不是单击期间在图形的状态栏中显示信息会更酷。如果我以后有时间/兴趣,我会看看我能做什么。
    【解决方案2】:

    我确定您已经成功地做到了这一点。稍微(!)修改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

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 1970-01-01
      • 2012-05-12
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2021-04-08
      相关资源
      最近更新 更多