matplotlib-如何用节点的坐标绘制?
我目前正在解决车辆路径问题,我想基于散点图可视化我的解决方案。但是,例如,如何将 Node1 连接到 Node 5?
我想根据我的解决方案连接到特定节点。
编辑:我已尝试使用提到的 matplotlib,但我无法获取坐标。
代码编写如下。
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
# G = nx.Graph()
G = nx.DiGraph(directed=True)
G.add_edges_from(
[('0', '3'), ('3', '6'), ('6', '0'), ('0', '5'), ('5', '4'),
('4', '0'), ('0', '2'), ('2', '1'), ('1', '0')])
val_map = {'1': 1.0,
'5': 0.5714285714285714,
'6': 0.0}
values = [val_map.get(node, 0.25) for node in G.nodes()]
# nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values)
# nx.draw_networkx(G[, pos, arrows, with_labels])
options = {
'node_color': 'green',
'node_size': 800,
'width': 3,
'arrowstyle': '-|>',
'arrowsize': 20,
}
nx.draw_networkx(G, arrows=True, **options)
plt.show()
另外,从节点0-6分别给出节点的坐标:
data['locations'] = [(1106, 3368.76),
(97.497, 230.937), (55.514, 2920.53),
(44.019, 5588.47), (2499.09, 242.61),
(2652.1, 2932.21), (2640.87, 5615.41),]
PS。我遇到的另一个问题是,每当我运行它时,图表都会不断变化。我该如何修复它?
I am currently solving a vehicle routing problem and i want to visualise my solution base on the scatter plot. However, how do I connect lets say for example Node1 to Node 5?
I want to connect to a particular node base on my solution.
edit: I have tried using matplotlib as mentioned, but i can't get the coordinates.
The code is written as stated below.
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
# G = nx.Graph()
G = nx.DiGraph(directed=True)
G.add_edges_from(
[('0', '3'), ('3', '6'), ('6', '0'), ('0', '5'), ('5', '4'),
('4', '0'), ('0', '2'), ('2', '1'), ('1', '0')])
val_map = {'1': 1.0,
'5': 0.5714285714285714,
'6': 0.0}
values = [val_map.get(node, 0.25) for node in G.nodes()]
# nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values)
# nx.draw_networkx(G[, pos, arrows, with_labels])
options = {
'node_color': 'green',
'node_size': 800,
'width': 3,
'arrowstyle': '-|>',
'arrowsize': 20,
}
nx.draw_networkx(G, arrows=True, **options)
plt.show()
Also, the coordinates of the nodes are given as follows from node0-6 respectively:
data['locations'] = [(1106, 3368.76),
(97.497, 230.937), (55.514, 2920.53),
(44.019, 5588.47), (2499.09, 242.61),
(2652.1, 2932.21), (2640.87, 5615.41),]
PS. I have another problem with this is that the graph kepts changing whenever I run it. How do I make it fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
<
您需要在
pos
posnx.draw_networkx
中的关键字参数添加节点位置,如果不这样做,则每次绘制图形时,都会生成新的布局。POS需要是字典,带有节点名称为键,而节点坐标为值。
documentation of draw_networkx
You need to specify the
pos
keyword argument innx.draw_networkx
to add node positions, if you do not, then a new layout will be generated each time you draw the graph.pos needs to be a dictionary, with node names as keys, and node coordinates as values.