Bokeh和networkx在其文档中使用的示例正在运行。我不知道他们使用的数据,因此我使用的是我在下面找到并显示的“空手道俱乐部”数据。我不能认为这是相同的数据。 我从json文件中读取数据。
unnest
nx.set_node_attributes(G,nd_attrs,“ g_color”)
IN_LIST,NOT_IN_LIST =“红色”,“灰色”
对于start_node,end_node,_在G.edges中(data = True):
如果lst_big_nodes中的start_node:#或lst_big_nodes中的end_node:
edge_color = IN_LIST
其他:
edge_color = NOT_IN_LIST
edge_attrs [(start_node,end_node)] = edge_color
library(tidyverse)
iris %>%
group_by(Species) %>%
nest %>%
mutate(data = map(data, ~ .x %>%
select_if(is.numeric) %>%
mutate(Count = sum(rowSums(.))))) %>%
#or use reduce with sum
# mutate(Count = reduce(., `+`) %>% sum))) %>%
unnest
# A tibble: 150 x 6
# Species Sepal.Length Sepal.Width Petal.Length Petal.Width Count
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 setosa 5.1 3.5 1.4 0.2 507.
# 2 setosa 4.9 3 1.4 0.2 507.
# 3 setosa 4.7 3.2 1.3 0.2 507.
# 4 setosa 4.6 3.1 1.5 0.2 507.
# 5 setosa 5 3.6 1.4 0.2 507.
# 6 setosa 5.4 3.9 1.7 0.4 507.
# 7 setosa 4.6 3.4 1.4 0.3 507.
# 8 setosa 5 3.4 1.5 0.2 507.
# 9 setosa 4.4 2.9 1.4 0.2 507.
#10 setosa 4.9 3.1 1.5 0.1 507.
# ... with 140 more rows
我得到了错误:
nx.set_edge_attributes(G,edge_attrs,“ edge_color”)
用于(u,v,键),values.items()中的值: ValueError:没有足够的值可解压缩(预期3,得到2)
我确实尝试了很多不同的香草口味,而没有任何适用于边缘颜色的代码。我丢失了一些东西,但看不到。 谢谢您的帮助。
函数read_json_file来自read json graph networkx file
这是来自“空手道俱乐部”的数据集
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'