
- 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 - 建立徽標
- 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 - Quiver 圖
- Matplotlib 有用資源
- Matplotlib - 快速指南
- Matplotlib - 有用資源
- Matplotlib - 討論
Matplotlib - 縮放視窗
在資料視覺化/繪圖中,縮放視窗是指調整檢視以使特定物件或區域更大或更小的過程。此互動式功能在探索圖形、圖表或任何視覺表示時特別有用,使使用者能夠專注於感興趣的特定區域或全面檢視整個內容。
Matplotlib 中的縮放視窗
Matplotlib 的關鍵功能之一是其對事件處理的支援,它允許使用者將滑鼠點選等事件連線到繪圖中的特定操作。在本教程中,我們將探索 Matplotlib 中的縮放視窗事件處理,重點關注button_press_event以及如何使用它來建立可縮放視窗。
示例 1
此示例建立兩個圖形(源和縮放)。源圖形顯示散點圖,縮放圖形顯示初始的放大檢視。當在源圖形中單擊一個點時,on_press函式透過使用button_press_event觸發。此 on_press 函式調整可縮放圖形的限制,以在所單擊的點為中心建立縮放效果。
import matplotlib.pyplot as plt import numpy as np # Fixing random state for reproducibility np.random.seed(19601) # Create source and zoomable figures and axes figsrc, axsrc = plt.subplots(figsize=(3.7, 3.7)) figzoom, axzoom = plt.subplots(figsize=(3.7, 3.7)) # Set initial limits and titles for both axes axsrc.set(xlim=(0, 1), ylim=(0, 1), autoscale_on=False, title='Click to zoom') axzoom.set(xlim=(0.45, 0.55), ylim=(0.4, 0.6), autoscale_on=False, title='Zoom window') # Generate random data for scatter plots x, y, s, c = np.random.rand(4, 100) s *= 200 # Plot the scatter plots on both axes axsrc.scatter(x, y, s, c) axzoom.scatter(x, y, s, c) # Define the event handling function def on_press(event): if event.button != 1: return x, y = event.xdata, event.ydata axzoom.set_xlim(x - 0.1, x + 0.1) axzoom.set_ylim(y - 0.1, y + 0.1) figzoom.canvas.draw() # Connect the event handler to the source figure figsrc.canvas.mpl_connect('button_press_event', on_press) # Show the plots plt.show()
輸出
執行上述程式後,您將獲得以下輸出:

觀看下面的影片以觀察縮放視窗功能在此處的運作方式。

示例 2
讓我們再建立一個使用 Matplotlib 建立縮放視窗的示例。在此示例中,一個簡單的正弦波繪製在主軸上,並使用plt.axes()方法建立了一個較小的可縮放視窗。當您單擊主圖中的一個點時,on_press 函式將被觸發,調整縮放視窗的限制,以在所單擊的點為中心建立縮放效果。
import matplotlib.pyplot as plt import numpy as np # Generate data x = np.linspace(0, 10, 100) y = np.sin(x) # Create a figure and axis fig, ax = plt.subplots(figsize=(7, 4)) ax.plot(x, y, label='Sin(x)') ax.set_title('Zoom Window Example') ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.legend() # Create a zoomable window zoomed_ax = plt.axes([0.55, 0.25, 0.3, 0.3], facecolor='lightgoldenrodyellow') zoomed_ax.plot(x, y, label='Sin(x)') zoomed_ax.set_title('Zoomed Window') zoomed_ax.set_xlim(2, 4) zoomed_ax.set_ylim(0.5, 1) def on_press(event): if event.button != 1: return x, y = event.xdata, event.ydata zoomed_ax.set_xlim(x - 1, x + 1) zoomed_ax.set_ylim(y - 0.2, y + 0.2) fig.canvas.draw() # Connect the event handler to the figure fig.canvas.mpl_connect('button_press_event', on_press) plt.show()
輸出
執行上述程式後,您將獲得以下輸出:

觀看下面的影片以觀察縮放視窗功能在此處的運作方式。

廣告