import networkx as nximport matplotlib.pyplot as plt# 读取人物关系数据with open('characters.txt', 'r') as f: data = f.readlines()# 创建空的Graph对象G = nx.Graph()# 添加节点和边for line in data: characters = line.strip().split(',') for i in range(len(characters)): if not G.has_node(characters[i]): G.add_node(characters[i]) for j in range(i+1, len(characters)): if not G.has_node(characters[j]): G.add_node(characters[j]) if not G.has_edge(characters[i], characters[j]): G.add_edge(characters[i], characters[j])# 绘制图形pos = nx.spring_layout(G)nx.draw(G, pos, with_labels=True, font_size=10, node_size=500, node_color='lightblue', edge_color='gray')plt.show()
时间: 2023-05-13 13:07:36 浏览: 68
这是Python中导入networkx和matplotlib.pyplot模块的语句。networkx是一个用于创建、操作和研究复杂网络的Python库,而matplotlib.pyplot是一个用于绘制图形的Python库。