
- 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 - Sankey 類
matplotlib 中的Sankey 類用於建立桑基圖,在深入研究 Sankey 類之前,瞭解桑基圖的基礎知識至關重要。
桑基圖是一種強大的視覺化工具,用於表示資源、能量或資訊在不同實體或過程之間的流動。它使用不同寬度的箭頭來表示流動的數量,寬度與表示的數量成比例。請參見下面的圖片,瞭解簡單桑基圖的參考。

桑基圖的關鍵組成部分
節點 - 發生流動之間的實體或過程。
流 - 連線節點的箭頭,表示流動的數量。
標籤 - 與節點或流相關的描述。
Matplotlib 中的 Sankey 類
在 Matplotlib 中,Sankey() 類提供了一種方便的方法來建立這些圖表。以下是該類的語法:
語法
class matplotlib.sankey.Sankey(ax=None, scale=1.0, unit='', format='%G', gap=0.25, radius=0.1, shoulder=0.03, offset=0.15, head_angle=100, margin=0.4, tolerance=1e-06, **kwargs)
以下是使用 matplotlib Sankey 類建立桑基圖的步驟:
建立 Sankey 物件 - 這可以透過使用 Sankey() 類來完成,該類初始化 Sankey 類的例項,該例項將用於構建圖表。
新增流和標籤 - Sankey.add() 方法用於指定流和標籤。
完成和顯示 - Sankey.finish() 方法完成圖表,plt.show() 顯示圖表。
繪製基本的桑基圖
現在,讓我們使用預設設定構建一個簡單的桑基圖。下面的示例建立一個具有一個輸入和一個輸出流的圖表。流使用flows引數指定,標籤使用labels引數提供。
示例 1
這是一個建立具有一個輸入和一個輸出流的桑基圖的示例。
import matplotlib.pyplot as plt from matplotlib.sankey import Sankey sankey = Sankey() sankey.add(flows=[1, -1], labels=['input', 'output']) plt.title("Sankey Diagram") sankey.finish() plt.show()
輸出
執行上述程式後,將生成以下桑基圖:

示例 2
另一個示例演示瞭如何透過隱式呼叫 Sankey.add() 方法並將 finish() 附加到對該類的呼叫來建立簡單的圖表。import matplotlib.pyplot as plt from matplotlib.sankey import Sankey Sankey(flows=[0.25, 0.15, 0.60, -0.20, -0.15, -0.05, -0.50, -0.10], labels=['', '', '', 'First', 'Second', 'Third', 'Fourth', 'Fifth'], orientations=[-1, 1, 0, 1, 1, 1, 0, -1]).finish() plt.title("The default settings produce a diagram like this.") plt.show()
輸出
執行上述程式後,將生成以下桑基圖:

自定義桑基圖
Matplotlib Sankey 類提供更多自定義選項,包括將多個桑基圖連線在一起,在圖表中間放置標籤,隱式地將關鍵字引數傳遞給 PathPatch(),更改箭頭頭的角度,更改建立圖表後補丁和標籤的外觀等等。
示例
這是一個使用 matplotlib sankey() 類建立複雜的自定義桑基圖的示例。
import matplotlib.pyplot as plt from matplotlib.sankey import Sankey # Create a subplot fig = plt.figure() ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Customized Sankey Diagram with Two Systems") # Create a Sankey diagram sankey = Sankey(ax=ax, scale=0.01, offset=0.2, format='%.0f', unit='%') # Add flows and labels for the first system sankey.add(flows=[25, 0, 60, -10, -20, -5, -15, -10, -40], label='one', labels=['', '', '', 'First', 'Second', 'Third', 'Fourth','Fifth', 'Hurray!'], orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0], pathlengths=[0.25, 0.25, 0.25, 0.25, 0.25, 0.6, 0.25, 0.25, 0.25], patchlabel="Widget\nA") # Add flows and labels for the second system, connected to the first sankey.add(flows=[-25, 15, 10], label='two', orientations=[-1, -1, -1], prior=0, connect=(0, 0)) # Finish the Sankey diagram and apply customizations diagrams = sankey.finish() diagrams[-1].patch.set_hatch('/') diagrams[0].texts[-1].set_color('r') diagrams[0].text.set_fontweight('bold') # Display the diagram plt.legend() plt.show()
輸出
執行上述程式後,將生成以下桑基圖:

廣告