시작하기무료로 시작하기

시간에 따른 엣지 변화 수 그리기

이제 그래프를 그려 보겠습니다! 이전에 만들었던 모든 리스트는 이 연습 문제에도 미리 로드되어 있어요. 아래에 보이는 약간 복잡해 보이는 matplotlib 코드는 걱정하지 않으셔도 됩니다. 어떤 동작을 하는지 이해할 수 있도록 주석을 달아 두었습니다.

이 연습은 강의의 일부입니다

Python 중급 네트워크 분석

강의 보기

연습 안내

  • 시간에 따라 추가된 엣지 수를 그려 보세요. 이를 위해:
    • 리스트 내포를 사용해 added를 순회하고 edges_added라는 리스트를 만드세요. 리스트 내포의 출력 식은 len(g.edges())이며, 여기서 g는 반복 변수입니다.
    • ax1.plot()edges_added 리스트를 전달하세요.
  • 시간에 따라 제거된 엣지 수를 그리세요. 이번에도 리스트 내포를 사용하되, added 대신 removed를 순회하세요.
  • fractional_changesax2.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()
코드 편집 및 실행