import networkx as nx import matplotlib.pyplot as plt # 职工信息数据 employees = [ {"编号": 1, "姓名": "张伟", "部门": "技术部", "工资": 1}, {"编号": 2, "姓名": "李娜", "部门": "产品部", "工资": 2}, {"编号": 3, "姓名": "王刚", "部门": "市场部", "工资": 3}, {"编号": 4, "姓名": "赵敏", "部门": "财务部", "工资": 4}, {"编号": 5, "姓名": "钱峰", "部门": "销售部", "工资": 5}, {"编号": 6, "姓名": "孙丽", "部门": "人力资源部", "工资": 6}, {"编号": 7, "姓名": "周杰", "部门": "技术支持部", "工资": 7}, {"编号": 8, "姓名": "吴明", "部门": "设计部", "工资": 8}, {"编号": 9, "姓名": "郑强", "部门": "测试部", "工资": 9}, {"编号": 10, "姓名": "陈芳", "部门": "项目管理部", "工资": 10}, {"编号": 11, "姓名": "黄磊", "部门": "运维部", "工资": 11}, {"编号": 12, "姓名": "吴迪", "部门": "开发部", "工资": 12}, {"编号": 13, "姓名": "郑丽", "部门": "开发部", "工资": 13}, {"编号": 14, "姓名": "陈思", "部门": "安全部", "工资": 14}, {"编号": 15, "姓名": "黄蓉", "部门": "内容部", "工资": 15}, {"编号": 16, "姓名": "周杰伦", "部门": "法务部", "工资": 16}, {"编号": 17, "姓名": "吴彦祖", "部门": "客户关系部", "工资": 17}, {"编号": 18, "姓名": "郑秀文", "部门": "公关部", "工资": 18}, {"编号": 19, "姓名": "陈奕迅", "部门": "行政部", "工资": 19} ] # 创建一个有向图 G = nx.DiGraph() # 添加节点(员工) for emp in employees: G.add_node(emp["姓名"], department=emp["部门"], salary=emp["工资"]) # 添加边(假设每个部门的员工与其他部门的员工有联系) departments = set(emp["部门"] for emp in employees) for dept1 in departments: for dept2 in departments: if dept1 != dept2: for emp1 in [emp for emp in employees if emp["部门"] == dept1]: for emp2 in [emp for emp in employees if emp["部门"] == dept2]: G.add_edge(emp1["姓名"], emp2["姓名"]) # 绘制关系图 plt.figure(figsize=(12, 12)) pos = nx.spring_layout(G, seed=42) # 使用Spring布局 nx.draw(G, pos, with_labels=True, node_size=2000, node_color="skyblue", font_size=10, font_weight="bold", edge_color="gray") plt.title("Employee Relationship Graph", fontsize=16) plt.show()