Matplotlib - 透视镜



透视镜通常指具有反射表面的物体,例如镜子,人们可以通过它观察自己的倒影或周围环境。

在图形用户界面的术语中,“透视镜”有时用来描述一个功能,该功能提供对系统或应用程序特定方面的详细视图或洞察。 Looking Glass Intro

Matplotlib中的透视镜

在Matplotlib的上下文中,透视镜是一个GUI应用程序或示例,它实现了一个交互式圆形窗口,可以显示或隐藏Matplotlib绘图的部分内容。此透视镜示例使用Matplotlib的patches模块创建一个交互式圆形窗口。这种交互性允许用户动态地探索底层数据。

本教程演示如何创建一个交互式圆形窗口(类似于透视镜),可以移动它来显示或隐藏其下方的绘图部分。

定义和可视化初始绘图

首先使用patches.Circle()类对象定义一个预定义的透视镜。

以下是初始绘图外观的设置:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches

np.random.seed(19680801)

x, y = np.random.rand(2, 200)
fig, ax = plt.subplots(figsize=(7, 4))

circle_= patches.Circle((0.5, 0.5), 0.25, alpha=0.8, fc='yellow')
ax.add_patch(circle_)
ax.plot(x, y, alpha=0.2)

line, = ax.plot(x, y, alpha=1.0, clip_path=circle_)

ax.set_title("Left click and drag to move looking glass")

实现透视镜交互

让我们看看用于创建交互式透视镜的EventHandler类的实现。此类捕获鼠标事件,允许用户单击、拖动和重新定位透视镜。

class EventHandler:
   def __init__(self):
      # Connect event handlers to the figure canvas
      fig.canvas.mpl_connect('button_press_event', self.on_press)
      fig.canvas.mpl_connect('button_release_event', self.on_release)
      fig.canvas.mpl_connect('motion_notify_event', self.on_move)

      # Initialize the center coordinates of the circular window
      self.x0, self.y0 = circle_.center
      self.pressevent = None

   def on_press(self, event):
      # Check if the event occurred inside the plot area
      if event.inaxes != ax:
         return

      # Check if the click is inside the circular window
      if not circle_.contains(event)[0]:
         return

      # Store the press event
      self.pressevent = event

   def on_release(self, event):
      # Reset the press event and update the center coordinates
      self.pressevent = None
      self.x0, self.y0 = circle_.center

   def on_move(self, event):
      # Check if a press event has occurred and if the mouse is still inside the plot
      if self.pressevent is None or event.inaxes != self.pressevent.inaxes:
         return

      # Calculate the change in coordinates
      dx = event.xdata - self.pressevent.xdata
      dy = event.ydata - self.pressevent.ydata

      # Update the center coordinates of the circle_ular window
      circle_.center = self.x0 + dx, self.y0 + dy

      # Update the clip path and redraw the plot
      line.set_clip_path(circle_)
      fig.canvas.draw()

运行实现

创建一个EventHandler类的实例,以在绘图上创建透视镜。

handler = EventHandler()

示例

让我们看看Matplotlib透视镜示例的完整代码。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches

np.random.seed(19680801)

# Generate random data for plot
x, y = np.random.rand(2, 200)

# Create a Matplotlib figure and axis
fig, ax = plt.subplots(figsize=(7, 4))

# Create a circular window (looking glass) and add it to the plot
circle_= patches.Circle((0.5, 0.5), 0.25, alpha=0.8, fc='yellow')
ax.add_patch(circle_)

# Plot the random data with transparency
ax.plot(x, y, alpha=0.2)

# Plot the same data again, but clip it to the circular window
line, = ax.plot(x, y, alpha=1.0, clip_path=circle_)

# Set the plot title
ax.set_title("Left click and drag to move looking glass")


class EventHandler:
   def __init__(self):
      # Connect event handlers to the figure canvas
      fig.canvas.mpl_connect('button_press_event', self.on_press)
      fig.canvas.mpl_connect('button_release_event', self.on_release)
      fig.canvas.mpl_connect('motion_notify_event', self.on_move)

      # Initialize the center coordinates of the circular window
      self.x0, self.y0 = circle_.center
      self.pressevent = None

   def on_press(self, event):
      # Check if the event occurred inside the plot area
      if event.inaxes != ax:
         return

      # Check if the click is inside the circular window
      if not circle_.contains(event)[0]:
         return

      # Store the press event
      self.pressevent = event

   def on_release(self, event):
      # Reset the press event and update the center coordinates
      self.pressevent = None
      self.x0, self.y0 = circle_.center

   def on_move(self, event):
      # Check if a press event has occurred and if the mouse is still inside the plot
      if self.pressevent is None or event.inaxes != self.pressevent.inaxes:
         return

      # Calculate the change in coordinates
      dx = event.xdata - self.pressevent.xdata
      dy = event.ydata - self.pressevent.ydata

      # Update the center coordinates of the circle_ular window
      circle_.center = self.x0 + dx, self.y0 + dy

      # Update the clip path and redraw the plot
      line.set_clip_path(circle_)
      fig.canvas.draw()

# Create an instance of the EventHandler class
handler = EventHandler()

# Display the plot
plt.show()

执行上述程序后,您将得到下图,左键单击鼠标并拖动透视镜以观察此示例的工作原理:

Looking Glass

观看下面的视频以观察此示例的工作原理。

Looking Glass GIF
广告