Python: get the degree of all nodes, then draw a boxplot in networkx
Asked Answered
V

1

5

I have a homework that need to call Networkx's function to get the degrees of all the nodes, and then draw a boxplot for these degrees.

But the boxplot is not show and have a error below:

"degree_values = list(my_degrees.values());

AttributeError: 'DegreeView' object has no attribute 'values'"

How to solve this problem? Thanks.

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edges_from([['9606.EN01','9606.EN02'],['9606.EN01','9606.EN03']])

fig = plt.figure();
nx.draw(G, with_labels=True, font_weight='bold')
plt.draw()

my_degrees = G.degree();
degree_values = list(my_degrees.values());
fig = plt.figure();
plt.boxplot(degree_values)
Venuti answered 8/3, 2018 at 7:43 Comment(0)
L
7

DegreeView isn't a dictionary (in NetworkX 2.1), but it is an iterator over (node, degree) pairs.

Try defining degree_values using

degree_values = [v for k, v in my_degrees]

Alternatively, if the order of degree_values doesn't matter, you can use

degree_values = dict(my_degrees).values()

Laconism answered 8/3, 2018 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.