
- 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 - 範圍滑塊
- 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 提供了一種處理滑鼠事件的機制,我們可以使用這些事件處理功能來實現自定義多邊形選擇功能。
Matplotlib 允許我們捕獲滑鼠事件,例如按鈕點選、滑鼠移動和釋放。我們可以利用這些事件來實現多邊形選擇工具,使用者可以互動式地圍繞所需資料點繪製多邊形。
關鍵概念
以下是多邊形選擇器小部件的關鍵概念。
Matplotlib 中的事件處理
Matplotlib 提供了一個靈活的事件處理系統,允許我們捕獲使用者與繪圖的互動。常見的事件包括滑鼠點選、按鍵和移動。
Path 類
matplotlib.path 模組中的 Path 類表示一系列連線的線段和曲線段,可用於定義形狀,例如多邊形。我們將使用它來定義並檢查點是否在選定的多邊形內。
用例和擴充套件
以下是多邊形選擇器小部件的用例和擴充套件。
- 區域選擇 - 自定義多邊形選擇器可用於透過允許使用者關注特定資料區域來定義和選擇繪圖中的特定感興趣區域。
- 資料過濾 - 選定的多邊形點可用於過濾和分析已繪製資料集中的特定資料子集。
- 與回撥函式的整合 - 我們可以透過整合響應多邊形選擇的回撥函式來擴充套件功能,從而允許根據選定區域執行自定義操作或分析。
- 動態視覺化 - 實現動態視覺化更新,例如突出顯示多邊形內的點,可以增強使用者體驗。
自定義
我們可以根據特定需求增強和自定義多邊形選擇器。例如,我們可以新增清除當前多邊形的功能,根據所選區域動態更新繪圖,或將其整合到更大的互動式應用程式中。
實現步驟
現在讓我們一起了解在 Matplotlib 中建立自定義多邊形選擇器的基本示例的步驟。
匯入必要的庫
首先,我們必須匯入 Matplotlib 和 NumPy 庫,用於繪圖和陣列操作。
import matplotlib.pyplot as plt import numpy as np
定義多邊形選擇器類
匯入必要的庫後,我們必須建立一個類來處理滑鼠事件,並允許使用者繪製多邊形。
class PolygonSelector: def __init__(self, ax): self.ax = ax self.polygon = None self.vertices = [] self.cid_press = ax.figure.canvas.mpl_connect('button_press_event', self.on_press) self.cid_release = ax.figure.canvas.mpl_connect('button_release_event', self.on_release) self.cid_motion = ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
定義事件回撥
我們必須定義事件回撥來實現處理滑鼠按下、釋放和移動事件的方法。
示例
def on_press(self, event): if event.inaxes != self.ax: return if event.button == 1: # Left mouse button if self.polygon is None: self.polygon = plt.Polygon([event.xdata, event.ydata], closed=False, fill=None, edgecolor='r') self.ax.add_patch(self.polygon) self.vertices.append((event.xdata, event.ydata)) else: self.vertices.append((event.xdata, event.ydata)) def on_release(self, event): if event.inaxes != self.ax: return if event.button == 1: # Left mouse button if len(self.vertices) > 2: self.vertices.append(self.vertices[0]) # Closing the polygon self.polygon.set_xy(np.array(self.vertices)) plt.draw() def on_motion(self, event): if event.inaxes != self.ax: return if event.button == 1 and self.polygon is not None: self.vertices[-1] = (event.xdata, event.ydata) self.polygon.set_xy(np.array(self.vertices)) plt.draw()
建立繪圖
接下來,我們必須生成樣本資料併為該生成的數建立繪圖。
x = np.random.rand(50) y = np.random.rand(50) fig, ax = plt.subplots() ax.scatter(x, y)
初始化多邊形選擇器並顯示繪圖
現在用繪圖的座標軸例項化PolygonSelector類,然後顯示繪圖。
polygon_selector = PolygonSelector(ax) plt.show()
現在讓我們將上述所有步驟組合在一起。
輸出

示例
import matplotlib.pyplot as plt from matplotlib.patches import Polygon from matplotlib.backend_bases import PickEvent import numpy as np class PolygonSelector: def __init__(self, ax): self.ax = ax self.points = [] self.polygon = None self.cid_click = ax.figure.canvas.mpl_connect('button_press_event', self.on_click) self.cid_key = ax.figure.canvas.mpl_connect('key_press_event', self.on_key_press) def on_click(self, event): if event.inaxes == self.ax: if event.button == 1: # Left mouse button self.points.append((event.xdata, event.ydata)) self.update_polygon() def on_key_press(self, event): if event.key == 'enter': print("Polygon points:", self.points) self.reset_polygon() def update_polygon(self): if self.polygon: self.polygon.remove() if len(self.points) > 2: self.polygon = Polygon(self.points, edgecolor='red', alpha=0.2, closed=True) self.ax.add_patch(self.polygon) self.ax.figure.canvas.draw() def reset_polygon(self): self.points = [] if self.polygon: self.polygon.remove() self.ax.figure.canvas.draw() # Create a scatter plot with random data np.random.seed(42) x_data = np.random.rand(50) y_data = np.random.rand(50) fig, ax = plt.subplots() ax.scatter(x_data, y_data) # Initialize the PolygonSelector polygon_selector = PolygonSelector(ax) plt.show()
輸出

示例
這是一個在 matpltlib 繪圖上建立多邊形選擇器的另一個示例。
import matplotlib.pyplot as plt from matplotlib.patches import Polygon from matplotlib.backend_bases import PickEvent import numpy as np class PolygonSelector: def __init__(self, ax): self.ax = ax self.points = [] self.polygon = None self.highlighted_points = None self.cid_click = ax.figure.canvas.mpl_connect('button_press_event', self.on_click) self.cid_key = ax.figure.canvas.mpl_connect('key_press_event', self.on_key_press) def on_click(self, event): if event.inaxes == self.ax: if event.button == 1: # Left mouse button self.points.append((event.xdata, event.ydata)) self.update_polygon() def on_key_press(self, event): if event.key == 'enter': self.highlight_points_inside_polygon() self.reset_polygon() def update_polygon(self): if self.polygon: self.polygon.remove() if len(self.points) > 2: self.polygon = Polygon(self.points, edgecolor='red', alpha=0.2, closed=True) self.ax.add_patch(self.polygon) self.ax.figure.canvas.draw() def reset_polygon(self): self.points = [] if self.polygon: self.polygon.remove() self.ax.figure.canvas.draw() def highlight_points_inside_polygon(self): if self.highlighted_points: for point in self.highlighted_points: point.set_markersize(5) self.highlighted_points = [] if self.polygon: path = self.polygon.get_path() points_inside_polygon = self.ax.plot([], [], 'o', markersize=8, markerfacecolor='yellow')[0] for i in range(len(self.ax.collections)): if isinstance(self.ax.collections[i], PickEvent): xs, ys = self.ax.collections[i].get_offsets().T points_inside = path.contains_points(np.column_stack((xs, ys))) self.highlighted_points.extend( self.ax.plot( xs[points_inside], ys[points_inside], 'o', markersize=8, markerfacecolor='yellow')) self.ax.figure.canvas.draw() # Create a scatter plot with random data np.random.seed(42) x_data = np.random.rand(50) y_data = np.random.rand(50) fig, ax = plt.subplots() ax.scatter(x_data, y_data) # Initialize the PolygonSelector polygon_selector = PolygonSelector(ax) plt.show()
輸出

填充多邊形內的區域
在這個例子中,我們使用 matplotlib 庫填充多邊形內的區域。
示例
import matplotlib.pyplot as plt from matplotlib.collections import PatchCollection from matplotlib.patches import Polygon import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots(1) polygon = Polygon(np.random.rand(6, 2), closed=True, alpha=1) collection = PatchCollection([polygon]) ax.add_collection(collection) plt.show()
輸出

注意 - 要在繪圖上繪製多邊形,請拖動游標,座標值將根據使用者選擇而變化。