I calculated degree centrality for the nodes with the weight of the links between the nodes. The next task is to draw a graph with nodes of different sizes. For example, if the degree centrality > 4, the node size = 1500, if < 4 = 500. Help understand where the error is.
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
A = [[0, 1.51, 0, 1.71, 0],
[0, 0, 2.11, 1.81, 2.31],
[0, 0, 0, 1.31, 1.41],
[0, 0, 0, 0, 1.11],
[0, 0, 0, 0, 0]]
G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
layout = nx.spring_layout(G)
labels = nx.get_edge_attributes(G, "weight")
# a list of the node labels in the right order
raw_labels = ["A1", "K2", "D3", "E4", "Z30"]
lab_node = dict(zip(G.nodes, raw_labels))
print("Degree centrality weight")
d = G.degree(weight='weight')
print(d)
for x in d:
if x[1] > 4:
large = x
print (large)
else:
small = x
print (small)
nx.draw(G, layout)
nx.draw_networkx_nodes(G, layout, edgelist=large, node_size=100)
nx.draw_networkx_nodes(G, layout, edgelist=small, node_size=1500)
nx.draw_networkx_edge_labels(G, layout, edge_labels=labels)
nx.draw_networkx_labels(G, layout, labels=lab_node, font_size=10, font_family='sans-serif')
plt.show()
The following code works. In your code were some issues: first like already Joel raised in the comments, you used small
and large
as variables, but wanted them to be list. Second, you have used edgelist
instead of nodelist
in draw_networkx_nodes
. I replaced the nx.draw
with nx.draw_networkx_edges
(and added plt.axis("off")
) to allow other users drawing smaller or larger node sizes than the default size, because smaller sizes would not work with nx.draw
.
As last personal recommendation, I would replace variable names, such as d
, G
, or small
, with longer self explanatory names, like node_degrees
, graph
, node_with_low_degrees
.
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
A = [[0, 1.51, 0, 1.71, 0],
[0, 0, 2.11, 1.81, 2.31],
[0, 0, 0, 1.31, 1.41],
[0, 0, 0, 0, 1.11],
[0, 0, 0, 0, 0]]
G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
layout = nx.spring_layout(G)
labels = nx.get_edge_attributes(G, "weight")
# a list of the node labels in the right order
raw_labels = ["A1", "K2", "D3", "E4", "Z30"]
lab_node = dict(zip(G.nodes, raw_labels))
print("Degree centrality weight")
d = G.degree(weight='weight')
print(d)
large = []
small = []
for node in G:
if d[node] > 4:
large.append(node)
else:
small.append(node)
print("Small", small)
print("Large", large)
nx.draw_networkx_edges(G, layout)
nx.draw_networkx_nodes(G, layout, nodelist=large, node_size=100)
nx.draw_networkx_nodes(G, layout, nodelist=small, node_size=1500)
nx.draw_networkx_edge_labels(G, layout, edge_labels=labels)
nx.draw_networkx_labels(G, layout, labels=lab_node, font_size=10, font_family='sans-serif')
plt.axis("off")
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.