为NetworkX中的每个路径分配颜色 [英] Assign a color to each path in NetworkX

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

问题描述

我有汽车,城市和路线.每个城市都是一个节点.每条路线都是汽车产生的路径.

不同的汽车将具有不同的路径,有时可能会相交(这意味着不同的汽车在其路径中找到了同一座城市),

我将用所有城市和所有不同的路径绘制一个图,并用图方式绘制该图.示例:

 城市列表:CityA -CityB -CityD -CityZ -CityK汽车清单:Car1,Car2路线:Car1将有一条穿过cityA-cityB-cityD的路径,该路径将被涂成红色Car2将通过cityZ-cityA-cityK设置一条路径,该路径将显示为蓝色 

使用 networkx.classes.function.add_path 无法实现,因为我不会保留有关不同汽车的信息,只会有连接节点的列表:

 与前面的示例一样,add_path,G.edges():[(CityA-CityB),(CityB-CityD),(CityZ-CityA),(CityA-CityK)] 

我不确定用networkx是否可以实现我想要的东西.

一种绘图解决方案只是将列表传递给plotly,但是这样做我什至不使用NetworkX,下一步是分析图形.

解决方案

您可以在NetworkX中设置节点和边属性,然后将其用于例如自定义绘图的某些方面.在这种情况下,您可以将颜色属性设置为图形的边缘,并使用该属性在

I have cars,cities and routes. Every city is a node. Every route is a path generated by a car.

Different cars will have different path, sometimes paths could be intersected (which means differents cars have found the same city in they path), sometimes not.

I would rappresent a graph with all the cities and all the different path and plot the graph with plotly. Example:

List of cities: CityA -CityB -CityD -CityZ -CityK
List of cars: Car1, Car2

Routes:
Car1 will have a path through   cityA - cityB - cityD  this path will be colored in red
Car2 will have a path though    cityZ - cityA - cityK  this path will be colored in blue

Using networkx.classes.function.add_path I can't achive this because I will not preserve the information about different cars, there will be only the list of connected node:

As in the previous example add_path, G.edges(): [(CityA-CityB),(CityB-CityD),(CityZ-CityA),(CityA-CityK)]

I am not sure if what I am looking for could be achived with networkx.

A solution to plot is just passing the list to plotly but doing so I will not even use NetworkX and the next steps is to analize the graph.

解决方案

You can set node and edge attributes in NetworkX, which you can then use for instance to customize certain aspects of the plot. In this case, you could set a color attribute to the edges of the graph, and use this attribute to set the edge_color in nx.draw. Here's how you could do this with the example paths you've shared:

import networkx as nx
from matplotlib import pyplot as plt

path_car1 = ['cityA','cityB','cityD']
path_car2 = ['cityZ','cityA','cityK']

paths = [path_car1, path_car2]
colors = ['Red','Blue']

Now create a directed graph, and iterate over the lists of paths, and assigned colors to add them as edges, an corresponding attributes:

G = nx.DiGraph()
for path, color in zip(paths, colors):
    for edge in zip(path[:-1], path[1:]):
        G.add_edge(*edge, color=color)

You can get the values of a given attribute for all edges with:

edge_colors = nx.get_edge_attributes(G, 'color')

Now when plotting you can set the edge colors through the edge_color argument:

plt.figure(figsize=(10,7))
pos = nx.spring_layout(G, scale=20)
nx.draw(G, pos, 
        node_color='black',
        with_labels=True, 
        node_size=1200,
        edgelist=G.edges(),
        edge_color=edge_colors.values(),
        arrowsize=15,
        font_color='white',
        width=3,
        alpha=0.9)

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

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