Get startedGet started for free

Degree centrality distribution

The degree of a node is the number of neighbors that it has. The degree centrality is the number of neighbors divided by all possible neighbors that it could have. Depending on whether self-loops are allowed, the set of possible neighbors a node could have could also include the node itself.

The nx.degree_centrality(G) function returns a dictionary, where the keys are the nodes and the values are their degree centrality values.

The degree distribution degrees you computed in the previous exercise using the list comprehension has been pre-loaded.

This exercise is part of the course

Introduction to Network Analysis in Python

View Course

Exercise instructions

  • Compute the degree centrality of the Twitter network T.
  • Using plt.hist(), plot a histogram of the degree centrality distribution of T. This can be accessed using list(deg_cent.values()).
  • Plot a histogram of the degree distribution degrees of T. This is the same list you computed in the last exercise.
  • Create a scatter plot with degrees on the x-axis and the degree centrality distribution list(deg_cent.values()) on the y-axis.

Hands-on interactive exercise

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

# Import matplotlib.pyplot
import matplotlib.pyplot as plt

# Compute the degree centrality of the Twitter network: deg_cent
deg_cent = ____

# Plot a histogram of the degree centrality distribution of the graph.
plt.figure()
____
plt.show()

# Plot a histogram of the degree distribution of the graph
plt.figure()
____
plt.show()

# Plot a scatter plot of the centrality distribution and the degree distribution
plt.figure()
____
plt.show()
Edit and Run Code