mesa/可视化测试.py

61 lines
1.7 KiB
Python
Raw Normal View History

2024-09-29 16:41:34 +08:00
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import rcParams
# 设置中文字体和符号显示
rcParams['font.sans-serif'] = ['SimHei'] # 或者使用 ['Microsoft YaHei']
rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# 定义节点和边
nodes = {
0: '原材料供应商',
1: '零部件制造商',
2: '产品制造商',
3: '分销商',
4: '零售商'
}
edges = [
(0, 1), # 原材料供应商 -> 零部件制造商
(1, 2), # 零部件制造商 -> 产品制造商
(2, 3), # 产品制造商 -> 分销商
(3, 4) # 分销商 -> 零售商
]
# 定义节点的三维坐标 (x, y, z)
positions = {
0: (0, 0, 0), # 原材料供应商
1: (1, 0, 1), # 零部件制造商
2: (2, 0, 2), # 产品制造商
3: (3, 0, 3), # 分销商
4: (4, 0, 4) # 零售商
}
# 创建3D图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制节点
for node, (x, y, z) in positions.items():
ax.scatter(x, y, z, color='b', s=100) # 绘制每个节点
ax.text(x, y, z, nodes[node], size=12, zorder=1, color='k') # 添加节点标签
# 绘制边(箭头)
for start, end in edges:
start_pos = positions[start]
end_pos = positions[end]
ax.plot([start_pos[0], end_pos[0]],
[start_pos[1], end_pos[1]],
[start_pos[2], end_pos[2]], color='r') # 连接每对节点
# 设置坐标轴标签
ax.set_xlabel('X轴')
ax.set_ylabel('Y轴')
ax.set_zlabel('Z轴')
# 设置视角以便更好地观察3D图形
ax.view_init(elev=20., azim=-35) # 角度可根据需要调整
# 显示3D图形
plt.show()