Matplotlib - 路径编辑器



路径编辑器是一个应用程序,允许用户在图形环境中交互式地编辑和操作路径。在 Matplotlib 的上下文中,路径编辑器通常指的是一个图形用户界面 (GUI) 应用程序,它有助于编辑使用 Matplotlib 的 Path 类定义的路径。

在深入研究路径编辑器之前,了解 Matplotlib 路径的基础知识至关重要。路径是 Matplotlib 中的一个基本对象,它包含各种元素,例如线段、曲线和matplotlib.patches 模块中的形状。路径通过指定一系列命令(例如 moveto、lineto 和 curveto)来定义复杂的轮廓,提供了一种通用的方法。

Matplotlib 提供了一个强大的Path 类,作为创建和操作可视化中路径的基础。

分步实施

在本教程中,我们将探索 Matplotlib 路径编辑器,这是一个跨 GUI 应用程序,它使用 Matplotlib 的事件处理功能来交互式地编辑和修改画布上的路径。

创建 PathInteractor 类

创建一个路径编辑器 (PathInteractor) 类来处理与已定义路径的交互。此类包括用于切换顶点标记(使用“t”键)、拖动顶点以及响应鼠标和键盘事件的方法。

示例

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.backend_bases import MouseButton
from matplotlib.patches import PathPatch
from matplotlib.path import Path

class PathInteractor:

   showverts = True
   # max pixel distance to count as a vertex hit
   epsilon = 5  

   def __init__(self, pathpatch):
      # Initialization and event connections
      self.ax = pathpatch.axes
      canvas = self.ax.figure.canvas
      self.pathpatch = pathpatch
      self.pathpatch.set_animated(True)

      x, y = zip(*self.pathpatch.get_path().vertices)

      self.line, = ax.plot(
         x, y, marker='o', markerfacecolor='r', animated=True)

      self._ind = None  # the active vertex

      canvas.mpl_connect('draw_event', self.on_draw)
      canvas.mpl_connect('button_press_event', self.on_button_press)
      canvas.mpl_connect('key_press_event', self.on_key_press)
      canvas.mpl_connect('button_release_event', self.on_button_release)
      canvas.mpl_connect('motion_notify_event', self.on_mouse_move)
      self.canvas = canvas

   def get_ind_under_point(self, event):
      # Return the index of the point closest to the event position or *None*
      xy = self.pathpatch.get_path().vertices
      xyt = self.pathpatch.get_transform().transform(xy)  # to display coords
      xt, yt = xyt[:, 0], xyt[:, 1]
      d = np.sqrt((xt - event.x)**2 + (yt - event.y)**2)
      ind = d.argmin()
      return ind if d[ind] < self.epsilon else None

   def on_draw(self, event):
      # Callback for draws.
      self.background = self.canvas.copy_from_bbox(self.ax.bbox)
      self.ax.draw_artist(self.pathpatch)
      self.ax.draw_artist(self.line)
      self.canvas.blit(self.ax.bbox)

   def on_button_press(self, event):
      # Callback for mouse button presses 
      if (event.inaxes is None
            or event.button != MouseButton.LEFT
            or not self.showverts):
         return
     self._ind = self.get_ind_under_point(event)

   def on_button_release(self, event):
      # Callback for mouse button releases 
      if (event.button != MouseButton.LEFT
            or not self.showverts):
         return
      self._ind = None

   def on_key_press(self, event):
      # Callback for key presses 
      if not event.inaxes:
         return
      if event.key == 't':
         self.showverts = not self.showverts
         self.line.set_visible(self.showverts)
         if not self.showverts:
            self._ind = None
      self.canvas.draw()

   def on_mouse_move(self, event):
      # Callback for mouse movements 
      if (self._ind is None
         or event.inaxes is None
         or event.button != MouseButton.LEFT
         or not self.showverts):
      return

      vertices = self.pathpatch.get_path().vertices

      vertices[self._ind] = event.xdata, event.ydata
      self.line.set_data(zip(*vertices))

      self.canvas.restore_region(self.background)
      self.ax.draw_artist(self.pathpatch)
      self.ax.draw_artist(self.line)
      self.canvas.blit(self.ax.bbox)

事件处理和画布交互

PathInteractor 类将各种回调连接到画布事件,使用户能够与已定义的路径进行交互。这些交互包括按下和释放鼠标按钮、拖动顶点以及使用按键切换顶点标记。

canvas.mpl_connect('draw_event', self.on_draw)
canvas.mpl_connect('button_press_event', self.on_button_press)
canvas.mpl_connect('key_press_event', self.on_key_press)
canvas.mpl_connect('button_release_event', self.on_button_release)
canvas.mpl_connect('motion_notify_event', self.on_mouse_move)

定义和可视化路径

首先定义一个预定义的路径,该路径由各种路径代码和顶点组成,这些代码和顶点是使用 Matplotlib Path 类创建的。然后,使用 PathPatch 实例在画布上可视化此路径,从而为绘图添加交互组件。

fig, ax = plt.subplots()

pathdata = [
   (Path.MOVETO, (1.58, -2.57)),
   (Path.CURVE4, (0.35, -1.1)),
   (Path.CURVE4, (-1.75, 2.0)),
   (Path.CURVE4, (0.375, 2.0)),
   (Path.LINETO, (0.85, 1.15)),
   (Path.CURVE4, (2.2, 3.2)),
   (Path.CURVE4, (3, 0.05)),
   (Path.CURVE4, (2.0, -0.5)),
   (Path.CLOSEPOLY, (1.58, -2.57)),
]

codes, verts = zip(*pathdata)
path = Path(verts, codes)
patch = PathPatch(
   path, facecolor='green', edgecolor='yellow', alpha=0.5)
ax.add_patch(patch)

运行路径编辑器

实例化 PathInteractor 类,设置绘图属性并显示绘图。用户现在可以交互式地拖动顶点,使用“t”键切换顶点标记,并观察实时更新。

interactor = PathInteractor(patch)
ax.set_title('drag vertices to update path')
ax.set_xlim(-3, 4)
ax.set_ylim(-3, 4)

plt.show()

示例

让我们看看 Matplotlib 路径编辑器的完整示例。

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.backend_bases import MouseButton
from matplotlib.patches import PathPatch
from matplotlib.path import Path


class PathInteractor:

   showverts = True
   # max pixel distance to count as a vertex hit
   epsilon = 5  

   def __init__(self, pathpatch):
      # Initialization and event connections
      self.ax = pathpatch.axes
      canvas = self.ax.figure.canvas
      self.pathpatch = pathpatch
      self.pathpatch.set_animated(True)

      x, y = zip(*self.pathpatch.get_path().vertices)

      self.line, = ax.plot(
         x, y, marker='o', markerfacecolor='r', animated=True)

      self._ind = None  # the active vertex

      canvas.mpl_connect('draw_event', self.on_draw)
      canvas.mpl_connect('button_press_event', self.on_button_press)
      canvas.mpl_connect('key_press_event', self.on_key_press)
      canvas.mpl_connect('button_release_event', self.on_button_release)
      canvas.mpl_connect('motion_notify_event', self.on_mouse_move)
      self.canvas = canvas

   def get_ind_under_point(self, event):
      # Return the index of the point closest to the event position or *None*
      xy = self.pathpatch.get_path().vertices
      xyt = self.pathpatch.get_transform().transform(xy)  # to display coords
      xt, yt = xyt[:, 0], xyt[:, 1]
      d = np.sqrt((xt - event.x)**2 + (yt - event.y)**2)
      ind = d.argmin()
         return ind if d[ind] < self.epsilon else None

   def on_draw(self, event):
      # Callback for draws.
      self.background = self.canvas.copy_from_bbox(self.ax.bbox)
      self.ax.draw_artist(self.pathpatch)
      self.ax.draw_artist(self.line)
      self.canvas.blit(self.ax.bbox)

   def on_button_press(self, event):
      # Callback for mouse button presses 
      if (event.inaxes is None
         or event.button != MouseButton.LEFT
         or not self.showverts):
         return
      self._ind = self.get_ind_under_point(event)

   def on_button_release(self, event):
      # Callback for mouse button releases 
      if (event.button != MouseButton.LEFT
         or not self.showverts):
         return
      self._ind = None

   def on_key_press(self, event):
      # Callback for key presses 
      if not event.inaxes:
         return
      if event.key == 't':
         self.showverts = not self.showverts
         self.line.set_visible(self.showverts)
         if not self.showverts:
            self._ind = None
      self.canvas.draw()

   def on_mouse_move(self, event):
      # Callback for mouse movements 
      if (self._ind is None
         or event.inaxes is None
         or event.button != MouseButton.LEFT
         or not self.showverts):
      return

      vertices = self.pathpatch.get_path().vertices

      vertices[self._ind] = event.xdata, event.ydata
      self.line.set_data(zip(*vertices))

      self.canvas.restore_region(self.background)
      self.ax.draw_artist(self.pathpatch)
      self.ax.draw_artist(self.line)
      self.canvas.blit(self.ax.bbox)

fig, ax = plt.subplots()

pathdata = [
   (Path.MOVETO, (1.58, -2.57)),
   (Path.CURVE4, (0.35, -1.1)),
   (Path.CURVE4, (-1.75, 2.0)),
   (Path.CURVE4, (0.375, 2.0)),
   (Path.LINETO, (0.85, 1.15)),
   (Path.CURVE4, (2.2, 3.2)),
   (Path.CURVE4, (3, 0.05)),
   (Path.CURVE4, (2.0, -0.5)),
   (Path.CLOSEPOLY, (1.58, -2.57)),
]

codes, verts = zip(*pathdata)
path = Path(verts, codes)
patch = PathPatch(
   path, facecolor='green', edgecolor='yellow', alpha=0.5)
ax.add_patch(patch)

interactor = PathInteractor(patch)
ax.set_title('drag vertices to update path')
ax.set_xlim(-3, 4)
ax.set_ylim(-3, 4)

plt.show()
输出

执行上述程序后,您将获得以下图形。按键盘上的“t”键。此操作会切换顶点标记的可见性。当顶点标记可见(按下“t”后)时,您可以使用鼠标拖动这些标记。观察拖动顶点如何影响路径的形状。

Path Editor

观看下面的视频,了解路径编辑器的工作原理。

Path Editor gif
广告