開始使用免費開始

隨時間繪製邊變化的數量

現在你要來畫一些圖!你先前建立的所有串列在這個練習中也都已經幫你載入好了。底下出現的一些進階 matplotlib 程式碼不用擔心:註解會協助你了解每一步在做什麼。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • 繪製隨時間新增邊的數量。做法如下:
    • 使用串列生成式迭代 added,建立名為 edges_added 的串列。此串列生成式的「輸出運算式」為 len(g.edges()),其中 g 是你的迭代變數。
    • edges_added 串列傳入 ax1.plot()
  • 繪製隨時間移除邊的數量。再次使用串列生成式,但這次要迭代 removed,而不是 added
  • 將分數變化量隨時間的趨勢傳入 ax2.plot() 作為引數來繪製。

動手互動練習

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

# Import matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)

# Plot the number of edges added over time
edges_added = [____(____) for ____ in ____]
plot1 = ax1.plot(____, label='added', color='orange')

# Plot the number of edges removed over time
edges_removed = [____(____) for ____ in ____]
plot2 = ax1.plot(____, label='removed', color='purple')

# Set yscale to logarithmic scale
ax1.set_yscale('log')  
ax1.legend()

# 2nd axes shares x-axis with 1st axes object
ax2 = ax1.twinx()

# Plot the fractional changes over time
plot3 = ax2.plot(____, label='fractional change', color='green')

# Here, we create a single legend for both plots
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines1 + lines2, labels1 + labels2, loc=0)
plt.axhline(0, color='green', linestyle='--')
plt.show()
編輯並執行程式碼