開始使用免費開始

Matrix 圖

現在來練習做一些視覺化。第一個是 Matrix 圖。在 Matrix 圖中,矩陣用來表示邊。

本練習屬於課程

Python 網路分析入門

檢視課程

練習說明

  • 針對最大連通元件子圖繪製 Matrix 視覺化,並依照作者的使用者群組編號分組。
    • 先用提供的 sorted() 函式搭配 nx.connected_components(G) 計算最大連通元件子圖。Python 內建的 sorted() 函式 會接收可疊代物件並回傳排序後的清單(預設為遞增)。因此,為了取得最大連通元件子圖,請在陳述式後加上 [-1] 進行切片。
    • 建立 matrixh。你需要將參數 graphgroup_by 分別指定為最大連通元件子圖與 'grouping'
    • matrix 圖繪製到畫面上。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import necessary modules
from nxviz import matrix
import matplotlib.pyplot as plt

# Calculate the largest connected component: largest_ccs
largest_ccs = sorted(____, key=lambda x: len(x))[-1]

# Create the customized Matrix plot: h
h = ____

# Draw the Matrix plot to the screen
plt.show()
編輯並執行程式碼