NetworkX中的颜色节点

时间:2019-11-28 14:04:43

标签: colors networkx holoviews

我使用NetworkX和holoview创建了一个图形,并且希望为节点使用不同的颜色。这里的代码: (首先,请安装python库:

  • networkX(pip install networkx)
  • holoview(pip install holoview)
  • bokeh(点安装bokeh)
  • matplotlib(pip install matplotlib)
import pandas as pd
# Ouverture fichier excel
data = pd.read_excel (r'C:\Yourpath\Sedan_jour_00.xlsx')  
data = data[["commune", "rao_libelle_voie", "rao_titre_point", "adr_l1_name"]][0:20]

xlsx文件的Google表格链接(在本地下载,并替换为路径):

https://docs.google.com/spreadsheets/d/1IQfFiv1I6lvGUDpT_ykvvodafk6zn8dcoFjIYDZPEls/export?format=xlsx&id=1IQfFiv1I6lvGUDpT_ykvvodafk6zn8dcoFjIYDZPEls

import hvplot.networkx as hvnx
import matplotlib.pyplot as plt
import networkx as nx
import holoviews as hv
from bokeh.models import HoverTool

G = nx.balanced_tree(3, 2)

traite = set()
#for L1 in data:
for index, L1 in data.iterrows():
    L1 = L1.values
    for l in range(0,len(L1)-1):
        if L1[l] not in traite:
            G.add_node(L1[l])
            traite.add(L1[l])
        if L1[l+1] not in traite:
            G.add_node(L1[l+1])
            G.add_edge(L1[l],L1[l+1])
            traite.add((L1[l],L1[l+1]))

G = hvnx.draw(G, node_size=1500, node_color='#33D1FF', inspection_policy="edges")
hvnx.show(G)

G.opts(tools=[HoverTool(tooltips=[('end', '@end_values')])])
hvnx.show(G)

结果还可以,但是我想在每个节点上使用不同的颜色 (我没有在这里进行试用,那没有用);

这是我想要的结果(用油漆绘制的绘图):

enter image description here

0 个答案:

没有答案