1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Network Analysis in Python

Exercise

Visualizing using Matrix plots

It is time to try your first "fancy" graph visualization method: a matrix plot. To do this, nxviz provides a MatrixPlot object.

nxviz is a package for visualizing graphs in a rational fashion. Under the hood, the MatrixPlot 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.

Instructions

100 XP
  • Import nxviz as nv.
  • Plot the graph T as a matrix plot. To do this:
    • Create the MatrixPlot object called m using the nv.MatrixPlot() function with T passed in as an argument.
    • Draw the m to the screen using the .draw() method.
    • 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!