Get Started

Visualizing using Matrix plots

It is time to try your first "fancy" graph visualization method: a matrix plot. To do this, nxviz provides a matrix() function. This function, like all of nxviz's top-level API functions, will return a matplotlib axes object that can be displayed with plt.show().

nxviz is a package for visualizing graphs in a rational fashion. Under the hood, the matrix function utilizes nx.to_numpy_matrix(G), which returns the matrix form of the graph. Here, each node is one column and one row, and an edge between the two nodes is indicated by the value 1. In doing so, however, only the weight metadata is preserved; all other metadata is lost, as you'll verify using an assert statement.

A corresponding nx.from_numpy_matrix(A) allows one to quickly create a graph from a NumPy matrix. The default graph type is Graph(); if you want to make it a DiGraph(), that has to be specified using the create_using keyword argument, e.g. (nx.from_numpy_matrix(A, create_using=nx.DiGraph)).

One final note, matplotlib.pyplot and networkx have already been imported as plt and nx, respectively, and the graph T has been pre-loaded. For simplicity and speed, we have sub-sampled only 100 edges from the network.

This is a part of the course

“Introduction to Network Analysis in Python”

View Course

Exercise instructions

  • Import matrix from nxviz.
  • Plot the graph T as a matrix plot. To do this:
    • Create the matrix plot called m using the nv.matrix() function with T passed in as an argument.
    • Display the plot using plt.show().
  • Convert the graph to a matrix format, and then convert the graph to back to the NetworkX form from the matrix as a directed graph. This has been done for you.
  • Check that the category metadata field is lost from each node. This has also been done for you, so hit 'Submit Answer' to see the results!

Hands-on interactive exercise

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

# Import nxviz
____

# Create the matrix plot: m
m = ____

# Display the plot
____

# Convert T to a matrix format: A
A = nx.to_numpy_matrix(T)

# Convert A back to the NetworkX form as a directed graph: T_conv
T_conv = nx.from_numpy_matrix(A, create_using=nx.DiGraph())

# Check that the `category` metadata field is lost from each node
for n, d in T_conv.nodes(data=True):
    assert 'category' not in d.keys()
Edit and Run Code