首页 > 解决方案 > 我在网络的边缘x 我需要的颜色不止一种

问题描述

Bokeh 和 networkx 在其文档中的示例正在运行。我不知道他们使用的数据,所以我使用的是我在下面找到并显示的“空手道俱乐部”数据。我不能假设这是相同的数据。我从 json 文件中读取数据。

import networkx as nx
import json

from bokeh.models.graphs import from_networkx
from networkx.readwrite import json_graph
import holoviews as hv
from bokeh.models import Circle, MultiLine
hv.extension('bokeh')

def read_json_file(filename):
        with open(filename) as f:
            js_graph = json.load(f)
        return json_graph.node_link_graph(js_graph)

G = read_json_file('/Users/Desktop/KCnet.json')

lst_big_nodes = []
i = 0
for lst in list(nx.connected_components(G)):
        if len(lst) > 7:  # if this connected then all nodes same color
            lst_big_nodes = lst

nd_attrs = {}
edge_attrs = {}

for nd, _ in G.nodes(data=True):
        if nd in lst_big_nodes:
            nd_attrs[nd] = '#f44171'
        else:
            nd_attrs[nd] = '#4286f4'

nx.set_node_attributes(G,nd_attrs,“g_color”)

IN_LIST, NOT_IN_LIST = "red", "gray" for start_node,end_node, _ in G.edges(data=True): if start_node in lst_big_nodes: # or end_node in lst_big_nodes: edge_color = IN_LIST
else: edge_color = NOT_IN_LIST edge_attrs[(start_node , end_node)] = edge_color

nx.set_edge_attributes(G, edge_attrs, "edge_color")

plot = figure(title="Netwkx", plot_width=1000 ,plot_height=800,  
x_range=(-1, 1), y_range=(-1, 1),
                  tools="pan,wheel_zoom,reset", toolbar_location="above")


graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))
line_alpha=0.8, line_width=1)
graph_renderer.node_renderer.glyph = Circle(size=8, fill_color="g_color")
graph_renderer.edge_renderer.glyph = MultiLine(line_color="edge_attrs", 
line_alpha=0.8, line_width=1)
plot.renderers.append(graph_renderer)

plot.xgrid.grid_line_color = None
plot.ygrid.grid_line_color = None

output_file("interactive_graphs.html")
show(plot)

我得到错误:

nx.set_edge_attributes(G,edge_attrs,“edge_color”)

for (u, v, key), value in values.items(): ValueError: not enough values to unpack (expected 3, got 2)

我确实尝试了很多不同的香草口味,但边缘颜色没有工作代码。我错过了一些东西,我没有看到它。谢谢您的帮助。

函数 read_json_file 来自读取 json graph networkx 文件

散景网络x文档:

这是来自“空手道俱乐部”的数据集

    {"nodes":
[
{"color": 1, "name": "Mr Hi", "faction": 1},
{"color": 1, "name": " 2", "faction": 1},
{"color": 1, "name": " 3", "faction": 1},
{"color": 1, "name": " 4", "faction": 1},
{"color": 1, "name": " 5", "faction": 1},
{"color": 1, "name": " 6", "faction": 1},
{"color": 1, "name": " 7", "faction": 1},
{"color": 1, "name": " 8", "faction": 1},
{"color": 1, "name": " 9", "faction": 2},
{"color": 1, "name": " 10", "faction": 2},
{"color": 1, "name": " 11", "faction": 1},
{"color": 1, "name": " 12", "faction": 1},
{"color": 1, "name": " 13", "faction": 1},
{"color": 1, "name": " 14", "faction": 1},
{"color": 1, "name": " 15", "faction": 2},
{"color": 1, "name": " 16", "faction": 2},
{"color": 1, "name": " 17", "faction": 1},
{"color": 1, "name": " 18", "faction": 1},
{"color": 1, "name": " 19", "faction": 2},
{"color": 1, "name": " 20", "faction": 1},
{"color": 1, "name": " 21", "faction": 2},
{"color": 1, "name": " 22", "faction": 1},
{"color": 1, "name": " 23", "faction": 2},
{"color": 1, "name": " 24", "faction": 2},
{"color": 1, "name": " 25", "faction": 2},
{"color": 1, "name": " 26", "faction": 2},
{"color": 1, "name": " 27", "faction": 2},
{"color": 1, "name": " 28", "faction": 2},
{"color": 1, "name": " 29", "faction": 2},
{"color": 1, "name": " 30", "faction": 2},
{"color": 1, "name": " 31", "faction": 2},
{"color": 1, "name": " 32", "faction": 2},
{"color": 1, "name": " 33", "faction": 2},
{"color": 1, "name": "John A", "faction": 2}
],
"links":[
{"source": 0, "target": 1, "weight": 4, "id": "e1"},
{"source": 0, "target": 2, "weight": 5, "id": "e2"},
{"source": 0, "target": 3, "weight": 3, "id": "e3"},
{"source": 0, "target": 4, "weight": 3, "id": "e4"},
{"source": 0, "target": 5, "weight": 3, "id": "e5"},
{"source": 0, "target": 6, "weight": 3, "id": "e6"},
{"source": 0, "target": 7, "weight": 2, "id": "e7"},
{"source": 0, "target": 8, "weight": 2, "id": "e8"},
{"source": 0, "target": 10, "weight": 2, "id": "e9"},
{"source": 0, "target": 11, "weight": 3, "id": "e10"},
{"source": 0, "target": 12, "weight": 1, "id": "e11"},
{"source": 0, "target": 13, "weight": 3, "id": "e12"},
{"source": 0, "target": 17, "weight": 2, "id": "e13"},
{"source": 0, "target": 19, "weight": 2, "id": "e14"},
{"source": 0, "target": 21, "weight": 2, "id": "e15"},
{"source": 0, "target": 31, "weight": 2, "id": "e16"},
{"source": 1, "target": 2, "weight": 6, "id": "e17"},
{"source": 1, "target": 3, "weight": 3, "id": "e18"},
{"source": 1, "target": 7, "weight": 4, "id": "e19"},
{"source": 1, "target": 13, "weight": 5, "id": "e20"},
{"source": 1, "target": 17, "weight": 1, "id": "e21"},
{"source": 1, "target": 19, "weight": 2, "id": "e22"},
{"source": 1, "target": 21, "weight": 2, "id": "e23"},
{"source": 1, "target": 30, "weight": 2, "id": "e24"},
{"source": 2, "target": 3, "weight": 3, "id": "e25"},
{"source": 2, "target": 7, "weight": 4, "id": "e26"},
{"source": 2, "target": 8, "weight": 5, "id": "e27"},
{"source": 2, "target": 9, "weight": 1, "id": "e28"},
{"source": 2, "target": 13, "weight": 3, "id": "e29"},
{"source": 2, "target": 27, "weight": 2, "id": "e30"},
{"source": 2, "target": 28, "weight": 2, "id": "e31"},
{"source": 2, "target": 32, "weight": 2, "id": "e32"},
{"source": 3, "target": 7, "weight": 3, "id": "e33"},
{"source": 3, "target": 12, "weight": 3, "id": "e34"},
{"source": 3, "target": 13, "weight": 3, "id": "e35"},
{"source": 4, "target": 6, "weight": 2, "id": "e36"},
{"source": 4, "target": 10, "weight": 3, "id": "e37"},
{"source": 5, "target": 6, "weight": 5, "id": "e38"},
{"source": 5, "target": 10, "weight": 3, "id": "e39"},
{"source": 5, "target": 16, "weight": 3, "id": "e40"},
{"source": 6, "target": 16, "weight": 3, "id": "e41"},
{"source": 8, "target": 30, "weight": 3, "id": "e42"},
{"source": 8, "target": 32, "weight": 3, "id": "e43"},
{"source": 8, "target": 33, "weight": 4, "id": "e44"},
{"source": 9, "target": 33, "weight": 2, "id": "e45"},
{"source": 13, "target": 33, "weight": 3, "id": "e46"},
{"source": 14, "target": 32, "weight": 3, "id": "e47"},
{"source": 14, "target": 33, "weight": 2, "id": "e48"},
{"source": 15, "target": 32, "weight": 3, "id": "e49"},
{"source": 15, "target": 33, "weight": 4, "id": "e50"},
{"source": 18, "target": 32, "weight": 1, "id": "e51"},
{"source": 18, "target": 33, "weight": 2, "id": "e52"},
{"source": 19, "target": 33, "weight": 1, "id": "e53"},
{"source": 20, "target": 32, "weight": 3, "id": "e54"},
{"source": 20, "target": 33, "weight": 1, "id": "e55"},
{"source": 22, "target": 32, "weight": 2, "id": "e56"},
{"source": 22, "target": 33, "weight": 3, "id": "e57"},
{"source": 23, "target": 25, "weight": 5, "id": "e58"},
{"source": 23, "target": 27, "weight": 4, "id": "e59"},
{"source": 23, "target": 29, "weight": 3, "id": "e60"},
{"source": 23, "target": 32, "weight": 5, "id": "e61"},
{"source": 23, "target": 33, "weight": 4, "id": "e62"},
{"source": 24, "target": 25, "weight": 2, "id": "e63"},
{"source": 24, "target": 27, "weight": 3, "id": "e64"},
{"source": 24, "target": 31, "weight": 2, "id": "e65"},
{"source": 25, "target": 31, "weight": 7, "id": "e66"},
{"source": 26, "target": 29, "weight": 4, "id": "e67"},
{"source": 26, "target": 33, "weight": 2, "id": "e68"},
{"source": 27, "target": 33, "weight": 4, "id": "e69"},
{"source": 28, "target": 31, "weight": 2, "id": "e70"},
{"source": 28, "target": 33, "weight": 2, "id": "e71"},
{"source": 29, "target": 32, "weight": 4, "id": "e72"},
{"source": 29, "target": 33, "weight": 2, "id": "e73"},
{"source": 30, "target": 32, "weight": 3, "id": "e74"},
{"source": 30, "target": 33, "weight": 3, "id": "e75"},
{"source": 31, "target": 32, "weight": 4, "id": "e76"},
{"source": 31, "target": 33, "weight": 4, "id": "e77"},
{"source": 32, "target": 33, "weight": 5, "id": "e78"}
]
}

标签: python-3.xnetworkxbokeh

解决方案


推荐阅读