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
廣告