
- Matplotlib 基礎
- Matplotlib - 首頁
- Matplotlib - 簡介
- Matplotlib - 與 Seaborn 的比較
- Matplotlib - 環境設定
- Matplotlib - Anaconda 發行版
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - 簡單繪圖
- Matplotlib - 儲存圖形
- Matplotlib - 標記
- Matplotlib - 圖形
- Matplotlib - 樣式
- Matplotlib - 圖例
- Matplotlib - 顏色
- Matplotlib - 顏色圖
- Matplotlib - 顏色圖歸一化
- Matplotlib - 選擇顏色圖
- Matplotlib - 顏色條
- Matplotlib - 文字
- Matplotlib - 文字屬性
- Matplotlib - 子圖示題
- Matplotlib - 圖片
- Matplotlib - 圖片蒙版
- Matplotlib - 註釋
- Matplotlib - 箭頭
- Matplotlib - 字型
- Matplotlib - 什麼是字型?
- 全域性設定字型屬性
- Matplotlib - 字型索引
- Matplotlib - 字型屬性
- Matplotlib - 刻度
- Matplotlib - 線性刻度和對數刻度
- Matplotlib - 對稱對數刻度和Logit刻度
- Matplotlib - LaTeX
- Matplotlib - 什麼是LaTeX?
- Matplotlib - 用於數學表示式的LaTeX
- Matplotlib - 註釋中的LaTeX文字格式
- Matplotlib - PostScript
- 啟用註釋中的LaTex渲染
- Matplotlib - 數學表示式
- Matplotlib - 動畫
- Matplotlib - 繪圖元素
- Matplotlib - 使用Cycler進行樣式設定
- Matplotlib - 路徑
- Matplotlib - 路徑效果
- Matplotlib - 變換
- Matplotlib - 刻度線和刻度標籤
- Matplotlib - 弧度刻度
- Matplotlib - 日期刻度
- Matplotlib - 刻度格式化器
- Matplotlib - 刻度定位器
- Matplotlib - 基本單位
- Matplotlib - 自動縮放
- Matplotlib - 反轉座標軸
- Matplotlib - 對數座標軸
- Matplotlib - Symlog
- Matplotlib - 單位處理
- Matplotlib - 帶有單位的橢圓
- Matplotlib - 脊柱
- Matplotlib - 座標軸範圍
- Matplotlib - 座標軸比例
- Matplotlib - 座標軸刻度
- Matplotlib - 座標軸格式化
- Matplotlib - Axes 類
- Matplotlib - 雙座標軸
- Matplotlib - Figure 類
- Matplotlib - 多圖
- Matplotlib - 網格
- Matplotlib - 面向物件介面
- Matplotlib - PyLab 模組
- Matplotlib - Subplots() 函式
- Matplotlib - Subplot2grid() 函式
- Matplotlib - 錨定繪圖元素
- Matplotlib - 手動等值線
- Matplotlib - 座標報告
- Matplotlib - AGG 過濾器
- Matplotlib - 帶狀框
- Matplotlib - 填充螺旋線
- Matplotlib - Findobj 演示
- Matplotlib - 超連結
- Matplotlib - 圖片縮圖
- Matplotlib - 使用關鍵字進行繪圖
- Matplotlib - 建立Logo
- Matplotlib - 多頁 PDF
- Matplotlib - 多程序
- Matplotlib - 列印標準輸出
- Matplotlib - 複合路徑
- Matplotlib - Sankey 類
- Matplotlib - MRI 與 EEG
- Matplotlib - 樣式表
- Matplotlib - 背景顏色
- Matplotlib - Basemap
- Matplotlib 事件處理
- Matplotlib - 事件處理
- Matplotlib - 關閉事件
- Matplotlib - 滑鼠移動
- Matplotlib - 點選事件
- Matplotlib - 滾動事件
- Matplotlib - 按鍵事件
- Matplotlib - 選擇事件
- Matplotlib - 透視鏡
- Matplotlib - 路徑編輯器
- Matplotlib - 多邊形編輯器
- Matplotlib - 定時器
- Matplotlib - Viewlims
- Matplotlib - 縮放視窗
- Matplotlib 小部件
- Matplotlib - 游標小部件
- Matplotlib - 帶註釋的游標
- Matplotlib - 按鈕小部件
- Matplotlib - 複選框
- Matplotlib - 套索選擇器
- Matplotlib - 選單小部件
- Matplotlib - 滑鼠游標
- Matplotlib - 多游標
- Matplotlib - 多邊形選擇器
- Matplotlib - 單選按鈕
- Matplotlib - RangeSlider
- Matplotlib - 矩形選擇器
- Matplotlib - 橢圓選擇器
- Matplotlib - 滑塊小部件
- Matplotlib - 區間選擇器
- Matplotlib - 文字框
- Matplotlib 繪圖
- Matplotlib - 條形圖
- Matplotlib - 直方圖
- Matplotlib - 餅圖
- Matplotlib - 散點圖
- Matplotlib - 箱線圖
- Matplotlib - 小提琴圖
- Matplotlib - 等高線圖
- Matplotlib - 3D 繪圖
- Matplotlib - 3D 等高線
- Matplotlib - 3D 線框圖
- Matplotlib - 3D 表面圖
- Matplotlib - 矢羽圖
- Matplotlib 有用資源
- Matplotlib - 快速指南
- Matplotlib - 有用資源
- Matplotlib - 討論
Matplotlib - 透視鏡
透視鏡通常指具有反射表面的物體,例如鏡子,人們可以透過它觀察自己的倒影或周圍環境。
在圖形使用者介面的術語中,“透視鏡”有時用來描述一個功能,該功能提供對系統或應用程式特定方面的詳細檢視或洞察。
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()
執行上述程式後,您將得到下圖,左鍵單擊滑鼠並拖動透視鏡以觀察此示例的工作原理:

觀看下面的影片以觀察此示例的工作原理。

廣告