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
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 providedsorted()function. Python's built-insorted()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
matrixploth. You have to specify the parametersgraphandgroup_byto be the largest connected component subgraph and'grouping', respectively. - Draw the
matrixplot to the screen.
- First, calculate the largest connected component subgraph by using the
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()