开始使用免费开始使用

矩阵图(Matrix plot)

现在来练习可视化。第一个是矩阵图(Matrix plot)。在矩阵图中,矩阵用来表示边。

本练习是课程的一部分

Python 网络分析入门

查看课程

练习说明

  • 对最大的连通分量子图绘制矩阵图,并按作者的用户组编号进行分组。
    • 首先,使用提供的 sorted() 函数包裹 nx.connected_components(G) 来计算最大的连通分量子图。Python 内置的 sorted() 函数 接受一个可迭代对象并返回排序后的列表(默认升序)。因此,要获取最大的连通分量子图,可在语句末尾使用切片 [-1]
    • 创建矩阵图对象 h。请将参数 graphgroup_by 分别设置为最大的连通分量子图以及 'grouping'
    • 将该矩阵图绘制到屏幕上。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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

# Calculate the largest connected component: largest_ccs
largest_ccs = sorted(____, key=lambda x: len(x))[-1]

# Create the customized Matrix plot: h
h = ____

# Draw the Matrix plot to the screen
plt.show()
编辑并运行代码