Bokeh和Holoviews网络可视化中的NetworkX节点属性颜色

时间:2018-05-14 01:49:02

标签: python networkx bokeh holoviews

我正在尝试使用bokehholoviews来创建交互式网络可视化。但是,我无法弄清楚如何将节点属性传递到Holoviews图中,然后通过属性值进行着色。我的networkx图表有许多不同的节点属性,最后我希望有一个下拉菜单,允许我选择颜色的属性,以及在悬停工具提示中显示的属性。但是,就目前而言,我只希望能够通过任何节点属性进行着色。

我一直在关注我能找到的所有bokeh examples和所有holoviews examples,但我似乎无法弄明白。 Facebook的例子看起来很有希望,但我无法相信我必须从networkxpandas中取出我的数据才能做到这一点。

如何调整下面的代码,按照NetworkX节点属性为节点着色?

import networkx as nx
import holoviews as hv

hv.extension('bokeh')

%opts Graph [width=400 height=400]

# create an example graph
g = nx.karate_club_graph()

# look at node attributes
g.nodes[0]
{'club': 'Mr. Hi'}

# load network into holoviews
graph = hv.Graph.from_networkx(g, nx.spring_layout)

# define color palette
colors = ['#000000']+hv.Cycle('Category20').values

# set plotting options
plot_opts = dict(color_index='club', 
                 xaxis=None, 
                 yaxis=None, 
                 show_frame=False)

# set stple options
style_opts = dict(node_size=15, 
                  cmap=colors,
                  edge_line_width=2, 
                  edge_alpha=0.3)

# apply options to plot
padding = {'x': (-1.2, 1.2), 'y': (-1.2, 1.2)}
graph = graph.redim.range(**padding).opts(style=style_opts, plot=plot_opts)

# show plot
graph

graph

0 个答案:

没有答案