如何在bokeh网络图中设置节点大小和颜色?

编程入门 行业动态 更新时间:2024-10-26 12:29:15
本文介绍了如何在bokeh网络图中设置节点大小和颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果以标准的空手道俱乐部为例,如何根据节点的度数更改节点的颜色和大小?

If we take the standard Karate Club example, how do I change the color and size of the nodes according to their degree?

import networkx as nx from bokeh.io import show, output_file from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes from bokeh.palettes import Spectral4 G=nx.karate_club_graph() plot = Plot(plot_width=400, plot_height=400, x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1)) plot.title.text = "Graph Interaction Demonstration" plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool()) graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0)) graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0]) graph_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color=Spectral4[2]) graph_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color=Spectral4[1]) graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5) graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5) graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5) graph_renderer.selection_policy = NodesAndLinkedEdges() graph_renderer.inspection_policy = EdgesAndLinkedNodes() plot.renderers.append(graph_renderer) output_file("interactive_graphs.html") show(plot)

我尝试将节点度数存储在列表中并将该列表输入到图形渲染器中,但是出现以下错误:

I tried storing the node degrees in a list and inputting that list into the graph renderer, but I got the following error:

# adjust node size according to degree, etc d = nx.degree(G) node_sizes = [] for i in d: _, value = i node_sizes.append(5*value) ... graph_renderer.node_renderer.glyph = Circle(size=node_sizes, fill_color=Spectral4[0]) >>> ValueError: expected an element of either String, Dict(Enum('expr', 'field', 'value', 'transform', 'units'), Either(String, Instance(Transform), Instance(Expression), Float)) or Float, got <the list>

对于颜色,我尝试替换此行:

And for the color, I tried replacing this line:

graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])

此行来自此处 :

from bokeh.transform import linear_cmap graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=linear_cmap('name', 'Spectral8', min(G.nodes()), max(G.nodes())))

但是会出现以下错误:

ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: name [renderer: GlyphRenderer(id='ca38c587-0e5e-4bcb-a433-85602d3f66ab', ...)] ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: name [renderer: GlyphRenderer(id='def645fc-ecae-41b1-94e0-2baec74976f5', ...)] ERROR:bokeh.core.validation.check:E-1010 (CDSVIEW_SOURCE_DOESNT_MATCH): CDSView used by Glyph renderer must have a source that matches the Glyph renderer's data source: GlyphRenderer(id='ac3d58b0-7a97-43c1-bfce-8e99b4286fa5', ...) ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: name [renderer: GlyphRenderer(id='02b10671-1446-452b-81a9-9ba34dab2317', ...)] ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: name [renderer: GlyphRenderer(id='62aa25ea-25a6-45ec-9931-25eca9870d6a', ...)] ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: name [renderer: GlyphRenderer(id='723305c7-3e97-4c25-8418-5a916f18a6c4', ...)] ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: OWNER_CLIENT_NO [renderer: GlyphRenderer(id='3fbaedfd-f2d9-48ea-bf7b-e86868c8438b', ...)] ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: name [renderer: GlyphRenderer(id='ca38c587-0e5e-4bcb-a433-85602d3f66ab', ...)]

并生成空白HTML.

编辑:我弄清楚了如何为节点着色.

I figured out how to color the nodes.

# add different colors for each node graph_renderer.node_renderer.data_source.data['color'] = list(G.nodes) graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=linear_cmap('color', 'Spectral8', min(G.nodes), max(G.nodes)))

这将产生:

所以,现在我只需要弄清楚如何更改节点大小.

So, now I only need to figure out how to change the node sizes.

推荐答案

您正在做稀薄的东西:)

You are doing thins nearly right :)

您应该创建一个 data_source渲染器并将其名称用于自动属性选择(我代码的另一部分在您的另一个问题):

Instead of manual list creation, you should create a data_source of renderer and use its name for automatic attribute selection (the other part of my code is in your another question):

gr = from_networkx( G, nx.circular_layout, scale=1, center=(0,0) ) gr.node_renderer.data_source.data['index'] = list(reversed(range(len(G)))) gr.node_renderer.data_source.data['colors'] = Spectral8 gr.node_renderer.glyph = Circle( size='index', fill_color='colors' )

将吸引您

更多推荐

如何在bokeh网络图中设置节点大小和颜色?

本文发布于:2023-07-18 20:09:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1148577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   图中   大小   颜色   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!