
- 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 - Quiver 圖
- Matplotlib 有用資源
- Matplotlib - 快速指南
- Matplotlib - 有用資源
- Matplotlib - 討論
Matplotlib - 3D 繪圖
3D 繪圖是一種以圖形格式表示三維資料的方式。它允許您在三個空間維度(表示為 X、Y 和 Z 座標)中視覺化資訊。在 3D 圖中,資料點不僅位於平面中,而且具有深度,從而建立資料集更詳細的表示。

Matplotlib 中的 3D 繪圖
在 Matplotlib 中,我們可以使用 **mpl_toolkits.mplot3d** 模組建立三維圖。此模組提供用於建立三維視覺化的工具,包括散點圖、線圖、表面圖等。這些圖提供了一種在三維空間中表示和探索資料點或數學函式的方式。您可以自定義顏色、標記、標籤和透視等方面,以更有效地傳達資訊。
我們可以將 **numpy** 庫與 **mpl_toolkits.mplot3d** 模組整合,以生成多維資料和不同的函式,例如 scatter、plot_surface 或 plot_wireframe。
mpl_toolkits.mplot3d 模組
Matplotlib 中的“mpl_toolkits.mplot3d”模組增強了庫的三維繪圖功能。它引入了“Axes3D”類,該類能夠建立 3D 子圖。此模組透過 scatter()(用於 3D 散點圖)、plot_surface()(用於表面圖)和 plot_wireframe()(用於線框表示)等函式,促進了三維資料視覺化。
3D 散點圖
Matplotlib 中的 3D 散點圖是一種視覺化,其中資料點在三維空間中表示為單個標記。每個資料點由三個值定義,分別對應於其沿 X、Y 和 Z 軸的位置。這些軸建立了一個三維網格,每個標記都放置在該空間中的指定座標處。我們可以使用 scatter() 函式建立此型別的圖。
示例
在以下示例中,我們使用 NumPy 生成隨機 3D 資料點,並使用藍色標記建立 3D 散點圖。我們在三維空間中顯示圖,其中 x、y 和 z 軸表示點的座標 -
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # Generating random 3D data np.random.seed(42) n_points = 100 x = np.random.rand(n_points) y = np.random.rand(n_points) z = np.random.rand(n_points) # Creating a 3D scatter plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(x, y, z, c='blue', marker='o') ax.set_xlabel('X Axis') ax.set_ylabel('Y Axis') ax.set_zlabel('Z Axis') ax.set_title('3D Scatter Plot') plt.show()
輸出
生成的圖顯示了曲線下逐漸的顏色過渡 -

3D 線圖
Matplotlib 中的 3D 線圖是一種圖形表示,顯示三維空間中一系列點之間的連線。與傳統 2D 線圖(其中點在平面上連線)不同,3D 線圖擴充套件到三個維度,在 X、Y 和 Z 軸上形成一條連續的線。
我們可以使用 plot() 函式在 matplotlib 中建立 3D 線圖。當我們將此函式與 projection='3d' 設定一起使用時,它能夠生成 3D 線圖。
示例
在這裡,我們透過基於引數方程定義座標(x、y 和 z)來生成 3D 線圖的資料。生成的圖在三維空間中顯示螺旋形。x、y 和 z 軸分別表示座標 -
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # Generating data for a 3D line plot theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) z = np.linspace(-2, 2, 100) r = z**2 + 1 x = r * np.sin(theta) y = r * np.cos(theta) # Creating a 3D line plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot(x, y, z, label='3D Line Plot') ax.set_xlabel('X Axis') ax.set_ylabel('Y Axis') ax.set_zlabel('Z Axis') ax.set_title('3D Line Plot') plt.show()
輸出
執行上述程式碼後,我們將獲得以下輸出 -

3D 表面圖
Matplotlib 中的 3D 表面圖是三維空間中數學函式或資料集的視覺表示。此圖不使用平面線或標記,而是使用連續的表面來顯示變數如何在兩個輸入維度(X 和 Y)上變化,並取決於第三個維度(Z)。我們可以使用 plot_surface() 函式建立此型別的圖。
示例
在這裡,我們透過計算網格上每個點到原點的歐幾里德距離的正弦來生成 3D 表面圖的資料。生成的圖可視化了一個基於正弦函式上升和下降的表面。x、y 和 z 軸分別表示座標和表面的高度 -
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # Generating data for a 3D surface plot x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) x, y = np.meshgrid(x, y) z = np.sin(np.sqrt(x**2 + y**2)) # Creating a 3D surface plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(x, y, z, cmap='viridis') ax.set_xlabel('X Axis') ax.set_ylabel('Y Axis') ax.set_zlabel('Z Axis') ax.set_title('3D Surface Plot') plt.show()
輸出
執行上述程式碼後,我們將獲得以下輸出 -

3D 條形圖
Matplotlib 中的 3D 條形圖是一種視覺表示,其中資料在三維空間中使用矩形條表示。與常規條形圖(其中條形沿兩個軸(X 和 Y)放置)類似,3D 條形圖添加了第三個維度(Z)來表示每個條形的高度或幅度。我們可以使用 bar3d() 函式建立此型別的圖。
示例
在下面的示例中,我們生成一個 3D 條形圖的資料,該圖在“x”和“y”方向上都有五個條形。每個條形的高度由“z”陣列中的值確定。生成的圖可視化了一組具有不同高度的三維條形,x、y 和 z 軸分別表示圖的維度 -
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # Generating data for a 3D bar plot x_pos = np.arange(1, 6) y_pos = np.arange(1, 6) x_pos, y_pos = np.meshgrid(x_pos, y_pos) z_pos = np.zeros_like(x_pos) z = np.array([[5, 8, 3, 6, 2], [1, 2, 3, 4, 5], [2, 3, 6, 7, 8], [5, 6, 7, 8, 9], [3, 4, 5, 7, 8]]) # Creating a 3D bar plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.bar3d(x_pos.flatten(), y_pos.flatten(), z_pos.flatten(), 0.8, 0.8, z.flatten(), shade=True) ax.set_xlabel('X Axis') ax.set_ylabel('Y Axis') ax.set_zlabel('Z Axis') ax.set_title('3D Bar Plot') plt.show()
輸出
執行上述程式碼後,我們將獲得以下輸出 -
