Matplotlib - 如何绘制节点坐标?
Matplotlib - How to plot with coordinates of the nodes?
我目前正在解决车辆路径问题,我想根据散点图可视化我的解决方案。但是,我如何将 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()
另外从node0-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。我还有另一个问题是,每当我 运行 它时,图表都会不断变化。我该如何修复它?
documentation of draw_networkx
Parameters
G graph
A networkx graph
pos dictionary, optional
A dictionary with nodes as keys and positions as values. If not specified a
spring layout positioning will be computed. See
networkx.drawing.layout for functions that compute node positions.
您需要在nx.draw_networkx
中指定pos
关键字参数来添加节点位置,如果不指定,则每次绘制图形时都会生成一个新的布局。
pos需要是一个字典,节点名称作为键,节点坐标作为值。
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
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()]
options = {
'node_color': 'green',
'node_size': 800,
'width': 3,
'arrowstyle': '-|>',
'arrowsize': 20,
}
# creating a variable for the locations, as "data" was undefined
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),]
# generating pos dictionary
pos = {str(i):location for i, location in enumerate(locations)}
# drawing graph, with positions included.
nx.draw_networkx(G, pos=pos, arrows=True, **options)
plt.show()
我目前正在解决车辆路径问题,我想根据散点图可视化我的解决方案。但是,我如何将 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()
另外从node0-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。我还有另一个问题是,每当我 运行 它时,图表都会不断变化。我该如何修复它?
documentation of draw_networkx
Parameters G graph A networkx graph pos dictionary, optional A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will be computed. See networkx.drawing.layout for functions that compute node positions.
您需要在nx.draw_networkx
中指定pos
关键字参数来添加节点位置,如果不指定,则每次绘制图形时都会生成一个新的布局。
pos需要是一个字典,节点名称作为键,节点坐标作为值。
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
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()]
options = {
'node_color': 'green',
'node_size': 800,
'width': 3,
'arrowstyle': '-|>',
'arrowsize': 20,
}
# creating a variable for the locations, as "data" was undefined
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),]
# generating pos dictionary
pos = {str(i):location for i, location in enumerate(locations)}
# drawing graph, with positions included.
nx.draw_networkx(G, pos=pos, arrows=True, **options)
plt.show()