IIabm/test.py

29 lines
1.0 KiB
Python
Raw Permalink Normal View History

2023-03-16 15:07:43 +08:00
import networkx as nx
import json
in_file = open("myfile.json", "r")
js_file = json.load(in_file)
in_file.close()
firm_network = nx.adjacency_graph(js_file)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimHei'
pos = nx.nx_agraph.graphviz_layout(firm_network, prog="twopi", args="")
node_label = nx.get_node_attributes(firm_network, 'Name')
# print(node_label)
node_degree = dict(firm_network.out_degree())
node_label = {
key: f"{node_label[key]} {node_degree[key]}"
for key in node_label.keys()
}
node_size = list(nx.get_node_attributes(firm_network, 'Revenue_Log').values())
node_size = list(map(lambda x: x**2, node_size))
edge_label = nx.get_edge_attributes(firm_network, "Product")
# multi(di)graphs, the keys are 3-tuples
edge_label = {(n1, n2): label for (n1, n2, _), label in edge_label.items()}
plt.figure(figsize=(12, 12), dpi=300)
nx.draw(firm_network, pos, node_size=node_size, labels=node_label, font_size=6)
nx.draw_networkx_edge_labels(firm_network, pos, edge_label, font_size=4)
plt.savefig("network1.png")