Get startedGet started for free

Matrix plot

Let's now practice making some visualizations. The first one will be the Matrix plot. In a Matrix plot, the matrix is the representation of the edges.

This exercise is part of the course

Introduction to Network Analysis in Python

View Course

Exercise instructions

  • Make a Matrix plot visualization of the largest connected component subgraph, with authors grouped by their user group number.
    • First, calculate the largest connected component subgraph by using the nx.connected_components(G) inside the provided sorted() function. Python's built-in sorted() function takes an iterable and returns a sorted list (in ascending order, by default). Therefore, to access the largest connected component subgraph, the statement is sliced with [-1].
    • Create the matrix plot h. You have to specify the parameters graph and group_by to be the largest connected component subgraph and 'grouping', respectively.
    • Draw the matrix plot to the screen.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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()
Edit and Run Code