我正在使用Bokeh(v0.13.0)渲染networkx图。我的节点被索引/键为字符串或分类。我正在尝试根据所选的组为节点着色。但是,Bokeh的CDSView的GroupFilter组kwarg仅允许一个字符串参数。因此,就我而言,我只能按一个节点进行过滤。组是否可以接受多个字符串arg?如果我的索引/关键字是整数,则可以使用IndexFilter列出多个索引/关键字,但这不是我想要的,因为我的网络节点全部由其字符串名称标识。
在下面的四个节点A,B,C,D的简单示例中,节点B被涂成红色。我也想将节点D也涂成红色。
任何人都可以给予的任何指示,帮助等,我们深表感谢!谢谢!
import networkx as nx
from bokeh.io import show, output_file
from bokeh.plotting import figure
from bokeh.models import MultiLine, Circle
from bokeh.models import ColumnDataSource, CDSView, GroupFilter
from bokeh.models.graphs import from_networkx
from bokeh.palettes import Category10_4
N = [('A','B'), ('B','C'), ('B','D')]
G = nx.Graph(N)
plot = figure(x_range=(-1.1,1.1), y_range=(-1.1,1.1))
graph = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))
graph.node_renderer.glyph = Circle(size=15, fill_color=Category10_4[0])
graph.node_renderer.selection_glyph = Circle(size=15,
fill_color=Category10_4[2])
graph.node_renderer.hover_glyph = Circle(size=15,fill_color=Category10_4[1])
graph.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8,
line_width=5)
graph.edge_renderer.selection_glyph = MultiLine(line_color=Category10_4[2],
line_width=5)
graph.edge_renderer.hover_glyph = MultiLine(line_color=Category10_4[1],
line_width=5)
plot.renderers.append(graph)
node_keys = list(graph.layout_provider.graph_layout.keys())
x, y = zip(*graph.layout_provider.graph_layout.values())
source = ColumnDataSource({'x': x, 'y': y,
'nkeys': [node_keys[i] for i in range(len(x))]})
view1 = CDSView(source=source, filters=[GroupFilter(column_name='nkeys',
group='B')])
view = plot.circle(x="x", y="y", size=15, color='red', source=source,
view=view1)
plot.renderers.append(view)
show(plot)