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()