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”
Exercise instructions
- Import
matrix
fromnxviz
. - Plot the graph
T
as a matrix plot. To do this:- Create the matrix plot called
m
using thenv.matrix()
function withT
passed in as an argument. - Display the plot using
plt.show()
.
- Create the matrix plot called
- 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()
This exercise is part of the course
Introduction to Network Analysis in Python
This course will equip you with the skills to analyze, visualize, and make sense of networks using the NetworkX library.
In this chapter, you'll be introduced to fundamental concepts in network analytics while exploring a real-world Twitter network dataset. You'll also learn about NetworkX, a library that allows you to manipulate, analyze, and model graph data. You'll learn about the different types of graphs and how to rationally visualize them.
Exercise 1: Introduction to NetworksExercise 2: What is a network?Exercise 3: Basics of NetworkX API, using Twitter networkExercise 4: Basic drawing of a network using NetworkXExercise 5: Queries on a graphExercise 6: Types of graphsExercise 7: Checking the un/directed status of a graphExercise 8: Specifying a weight on edgesExercise 9: Checking whether there are self-loops in the graphExercise 10: Network visualizationExercise 11: Visualizing using Matrix plotsExercise 12: Visualizing using Circos plotsExercise 13: Visualizing using Arc plotsWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.