Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用networkx和bokeh在Holoviews中按类为节点着色(并将类添加为vdim)_Python_Networkx_Bokeh_Holoviews - Fatal编程技术网

Python 使用networkx和bokeh在Holoviews中按类为节点着色(并将类添加为vdim)

Python 使用networkx和bokeh在Holoviews中按类为节点着色(并将类添加为vdim),python,networkx,bokeh,holoviews,Python,Networkx,Bokeh,Holoviews,因此,我尝试根据类别为节点着色,比如说categories=['a','b','c'] for comb in data: edges.append((comb['comb'][0], comb['comb'][1], comb['count'])) graph = hv.Graph(edges, vdims='weight') graph = layout_nodes(graph, layout=nx.layout.fruchterman_reingold_layout

因此,我尝试根据类别为节点着色,比如说
categories=['a','b','c']

for comb in data:
    edges.append((comb['comb'][0], comb['comb'][1], comb['count']))
      
graph = hv.Graph(edges, vdims='weight') 
graph = layout_nodes(graph, layout=nx.layout.fruchterman_reingold_layout, kwargs={'weight': 'weight'})
nodes_array = graph.nodes.array()
for node in nodes_array:
    (node) #add vdim to node here and set node_color to this vdim?
labels = hv.Labels(([i[0] for i in nodes_array], [i[1] for i in nodes_array], [i[2] for i in nodes_array]))

(graph.opts(width=800, height=800, node_color='weight')*labels.opts(text_font_size='8pt', text_color='blue')) 
我需要的是一种方法,我想我需要为每个节点设置类别,以某种方式将其定义为vdim,然后将node_颜色设置为该vdim

我看到了关于特定节点和类似节点的其他问题,但我不认为我可以定义节点(包括位置),因为我需要
fruchterman\u reingold\u布局
为我这样做

(如果有办法更改节点的大小,则可获得额外奖励)

编辑: 因此,我还尝试通过
add_dimension
添加维度,如这里的文档所示:

graph.nodes.添加维度('weight',dim\u pos=3,dim\u val=['a']*457)
457是我的节点数,因此这是正确的,但我得到以下错误:
ValueError:kdims:列表长度必须介于3和3之间(包括3)

另外,我也不知道该如何处理函数的结果,我想以某种方式将其设置为新节点?

回答我自己的问题,因为我找到了实现我想要的结果的方法。 与其说它是一个干净的解决方案,不如说它是一个变通方法,但它是有效的,因此在找到更好的答案之前,它可能会对其他人有所帮助

from holoviews.element.graphs import layout_nodes
import random
G = hv.Graph([
    ('a', 'b', 3),
    ('a', 'c', 0.2),
    ('c', 'd', 0.1),
    ('c', 'e', 0.7),
    ('c', 'f', 5),
    ('a', 'd', 0.3)
], vdims=['weight'])

G = layout_nodes(G, layout=nx.layout.fruchterman_reingold_layout, kwargs={'weight': 'weight'})
nodes_array = G.nodes.array()
newNodes_array = []
for node in nodes_array:
    newNodes_array.append((node[0], node[1], node[2], random.choice(['class1', 'class2', 'class3'])))
N = hv.Nodes(newNodes_array, vdims='class')

labels = hv.Labels(([i[0] for i in nodes_array], [i[1] for i in nodes_array], [i[2] for i in nodes_array]))
G*(N).opts(color = 'class', cmap='Set1')*labels
应用所需的
nx.layout.fruchterman\u reingold\u布局
后,将复制
G.nodes.array()
,因此可以向这些节点添加新维度,并将它们与图形一起打印。 输出: