为路径设置不同的颜色 [英] Set a different color for a path

查看:64
本文介绍了为路径设置不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从文本文件中提取的加权图 G:

I have a weighted graph,G, extracted from a text file:

i   j   distance
1   2   6000 
1   3   4000
2   1   6000
2   6   5000
....

我有特定的路线(不是最短路径),我想在图 G 上绘制,即 [1, 2, 6, 7] 从节点 1 开始,通过访问节点 2 和节点在节点 7 结束6. 这是我试过的代码.但是由于我也是 python 和 networkx 包的新手,我无法得到我想要的结果.

And I have specific a route (not a shortest path) that I want to plot on graph G, i.e. [1, 2, 6, 7] that starts from node 1, end at node 7 by visiting node 2 and node 6. Here the code I've tried. But since Im new in python and networkx package as well, I couldn't get the result that Im looking for.

G = nx.read_edgelist('Graph.txt', data=(('weight',float),))
r=[1,2,6,7]
edges=[]
route_edges=[(r[n], r[n+1]) for n in range (len(r)-1)]
G.add_nodes_from(r)
G.add_edges_from(route_edges)
edges.append(route_edges)
pos=nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos=pos)
nx.draw_networkx_labels(G, pos=pos)
nx.draw_networkx_edges(G,pos=pos,edgelist=edges)

我想用不同的颜色绘制整个边我定义的路径,并且我想为节点6添加不同的颜色.

I want to plot whole edges and the path that I defined with different colors and also I want to add different color to node 6.

推荐答案

要以不同颜色突出显示特定路径,只需为路径边缘设置不同的颜色边缘颜色即可.因为您可以将包含列表的节点对的集合定义为元组,并根据路径是否包含在集合中为图的边设置颜色或其他颜色:

To highlight a specific path in a different color, it's just a matter of setting a different color edge color for the path edges. For you can define a set containing the list's pairs of nodes as tuples, and set a color or another to the graphs' edges depending on whether the path is contained in the set:

#graph defined with from_pandas_edgelist for simplicity
G = nx.from_pandas_edgelist(df.rename(columns={'distance':'weight'}), 'i', 'j', 'weight', 
                            create_using=nx.DiGraph)

我添加了更多边缘,使情节看起来更清晰:

I've added some more edges so the plot seems a little clearer:

G.edges(data=True)
# OutEdgeDataView([(1, 2, {'weight': 6000}), (1, 4, {'weight': 3200}), (1, 3, {'weight': 4000}), 
#                  (2, 3, {'weight': 1000}), (2, 6, {'weight': 5000}), (4, 8, {'weight': 4000}), 
#                  (6, 7, {'weight': 3000})])

我们可以为边缘颜色定义一个字典,并为节点的颜色定义一个列表:

We can define a dictionary for the edge colors, and a list for the nodes' color as:

path =  [1, 2, 6, 7]
path_edges = set(zip(path[:-1], path[1:]))

# set edge colors
edge_colors = dict()
for edge in G.edges():
    if edge in path_edges:
        edge_colors[edge] = 'magenta'
        continue
    else:
        edge_colors[edge] = 'lightblue'

nodes = G.nodes()
node_colors = ['orange' if i != 6 else 'lightgreen' for i in nodes]

我们可以用它来绘制图表:

Which we could use to plot the graph as:

fig = plt.figure(figsize=(12,8))
pos = nx.spring_layout(G, scale=20)
nx.draw(G, pos, 
        nodelist=nodes,
        node_color=node_colors,
        edgelist=edge_colors.keys(), 
        edge_color=edge_colors.values(),
        node_size=800,
        width=4,alpha=0.6,
        arrowsize=20,
        with_labels=True)

这篇关于为路径设置不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆