python - Matplotlib:用鼠标画出矩形形状的选区

标签 python matplotlib mouse selection matplotlib-basemap

我希望能够使用鼠标事件在 matplotlib 图上绘制一个选择区域。我没有找到有关如何使用 python 执行此操作的信息。

最后,我希望能够在使用 matplotlib basemap 创建的 map 上用鼠标绘制感兴趣区域并检索角坐标。

有人有想法、示例和引用资料吗?

谢谢,

格雷格

class Annotate(object):
  def __init__(self):
      self.ax = plt.gca()
      self.rect = Rectangle((0,0), 1, 1, facecolor='None', edgecolor='green')
      self.x0 = None
      self.y0 = None
      self.x1 = None
      self.y1 = None
      self.ax.add_patch(self.rect)
      self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
      self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
      self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
  def on_press(self, event):
      print 'press'
      self.x0 = event.xdata
      self.y0 = event.ydata    
      self.x1 = event.xdata
      self.y1 = event.ydata
      self.rect.set_width(self.x1 - self.x0)
      self.rect.set_height(self.y1 - self.y0)
      self.rect.set_xy((self.x0, self.y0))
      self.rect.set_linestyle('dashed')
      self.ax.figure.canvas.draw()
  def on_motion(self,event):
      if self.on_press is True:
          return
      self.x1 = event.xdata
      self.y1 = event.ydata
      self.rect.set_width(self.x1 - self.x0)
      self.rect.set_height(self.y1 - self.y0)
      self.rect.set_xy((self.x0, self.y0))
      self.rect.set_linestyle('dashed')
      self.ax.figure.canvas.draw()
  def on_release(self, event):
      print 'release'
      self.x1 = event.xdata
      self.y1 = event.ydata
      self.rect.set_width(self.x1 - self.x0)
      self.rect.set_height(self.y1 - self.y0)
      self.rect.set_xy((self.x0, self.y0))
      self.rect.set_linestyle('solid')
      self.ax.figure.canvas.draw()
      print self.x0,self.x1,self.y0,self.y1
      return [self.x0,self.x1,self.y0,self.y1]

最佳答案

这是一个小例子,展示了如何使用鼠标在 matplotlib 绘图上绘制矩形。

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

class Annotate(object):
    def __init__(self):
        self.ax = plt.gca()
        self.rect = Rectangle((0,0), 1, 1)
        self.x0 = None
        self.y0 = None
        self.x1 = None
        self.y1 = None
        self.ax.add_patch(self.rect)
        self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)

    def on_press(self, event):
        print 'press'
        self.x0 = event.xdata
        self.y0 = event.ydata

    def on_release(self, event):
        print 'release'
        self.x1 = event.xdata
        self.y1 = event.ydata
        self.rect.set_width(self.x1 - self.x0)
        self.rect.set_height(self.y1 - self.y0)
        self.rect.set_xy((self.x0, self.y0))
        self.ax.figure.canvas.draw()

a = Annotate()
plt.show()

关于python - Matplotlib:用鼠标画出矩形形状的选区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12052379/

相关文章:

python - 这不是与装饰器的工作方式相反吗?

python - 调整网格 xy 比例等值线图 matplotlib

python - matplotlib 堆栈条按日期(月和年)分组

python - 在 python 中可视化数据

user-interface - 向鼠标缩放(例如谷歌地图)

python - 如何在不停止整个脚本的情况下停止数据测量

python - PyQt5 - 将小部件放置在 QTextEdit 的右上角

python - 用于查找可由列表中其他单词组成的最长单词的python代码的时间复杂度

python - 获取嵌套字典中所有项目的所有 parent 键

mouse - Godot:检测 Area2D 内部的 "mouse down"和 Area2D 外部的 "mouse up"