matplotlib-如何用节点的坐标绘制?

发布于 2025-01-21 02:46:09 字数 1477 浏览 1 评论 0原文

我目前正在解决车辆路径问题,我想基于散点图可视化我的解决方案。但是,例如,如何将 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.

enter image description here

edit: I have tried using matplotlib as mentioned, but i can't get the coordinates.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白首有我共你 2025-01-28 02:46:09

<

 参数
G图
网络图

POS词典,可选
带有节点作为键和位置的词典作为值。如果未指定 
将计算弹簧布局定位。看
NetworkX.Drawing.layout用于计算节点位置的函数。
 

您需要在pos pos nx.draw_networkx中的关键字参数添加节点位置,如果不这样做,则每次绘制图形时,都会生成新的布局。

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()

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.

You need to specify the pos keyword argument in nx.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.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文