开始使用免费开始使用

随时间绘制边变化的数量

现在您要开始绘图了!本练习中已为您加载好之前创建的所有列表。下面会出现一些较「花哨」的 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()
编辑并运行代码