from __future__ import print_function
import matplotlib.pyplot as plt
import networkx as nx
import MySQLdb
# #############################################################################
# Retrieve the data from remote server.
myDB = MySQLdb.connect(host="*,port=3306,user="mysql",passwd="***")
cHandler = myDB.cursor()
cHandler.execute("USE research_project")
cHandler.execute("SELECT * FROM students")
results = cHandler.fetchall()
G = nx.Graph()
for items in results:
G.add_node(items[0], attr_dict={'university_id': items[1], 'full_name': items[2]})
for node_r in G.nodes(data=True):
for node in G.nodes(data=True):
if node != node_r and node[1]['attr_dict']['university_id'] == node_r[1]['attr_dict']['university_id']:
G.add_edge(node[0], node_r[0], attr_dict=None)
nx.draw(G, with_labels=True)
plt.show()
我在一些小的数据集上测试了上面的内容,它似乎可以工作。
我有种预感,发生的事情与我向节点添加属性的方式有关。
上述解决方案的警告是,它在运行时速度非常慢。只要我能想出一个更快的解决方案,我就会更新我的答案。