開始使用免費開始

Arc 圖

接下來,讓我們用 Arc 圖來視覺化這個網路。你也會練習如何對圖中的節點進行排序。

注意:如果正確完成,這個練習執行大約需要 4–7 秒。

本練習屬於課程

Python 網路分析入門

檢視課程

練習說明

  • 以 GitHub 協作網路製作一個 Arc 圖,並依節點的 degree 來排序作者。做法如下:
    • 遍歷 G 中所有節點並包含中繼資料(指定 data=True)。
    • 在每次迴圈中,使用 nx.degree() 計算每個節點 n 的 degree,並設定其 'degree' 屬性。nx.degree() 接受兩個引數:圖形與節點。
    • 建立 arc 圖物件 a,指定兩個參數:graph 引數為 G,以及 sort_by 引數為 'degree',讓節點依此排序。
    • arc 圖顯示在螢幕上。

動手互動練習

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

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

# Iterate over all the nodes in G, including the metadata
for n, d in ____:

    # Calculate the degree of each node: G.node[n]['degree']
    ____ = ____

# Create the Arc plot: a
a = ____

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