散景中网络节点的多个字形

问题描述 投票:0回答:1

有没有办法绘制具有多个不同字形(圆形、矩形...)的网络图(由 python networkx 模块生成)?

到目前为止,我只找到了一种方法,使用“graph.node_renderer.glyph = Something”使所有节点字形相同,只是大小和颜色不同。

谢谢你。

python-3.x bokeh
1个回答
0
投票

为节点提供不同标记的一种方法是使用 Scatter 字形。

第 1 部分:使用带有指向数据源的标记属性的散点字形

所以,而不是

graph.node_renderer.glyph = Circle(size=20)

你也可以用它来画圆圈

graph.node_renderer.glyph = Scatter(size=20, marker="circle")

如果将

marker
部分更改为指向数据源,则可以使用多个不同的标记(见下文):

graph.node_renderer.glyph = Scatter(size=20, marker="marker")

此处的图表类型为 bokeh.models.GraphRenderer

第 2 部分:添加定义每个节点标记的数据源

然后,您需要为“标记”添加数据源:

# NOTE: make one item in `markers` per node in the graph (assuming 4-node graph) 
markers = ["circle", "square", "circle", "square"]
graph.node_renderer.data_source.add(markers, 'marker')

或者,您可能想使用字典一次定义整个数据源

graph.node_renderer.data_source.data = dict(
   # The 'index' is required by GraphRenderer. It must be a unique node identifier.
   index=node_ids,
   size=sizes,
   marker=markers
)

标记的不同选项在 bokeh.core.enums.MarkerType 中列出,分别是:

sterisk, circle, circle_cross, circle_dot, circle_x,

    circle_y, cross, dash, diamond, diamond_cross, diamond_dot, dot,

    hex, hex_dot, inverted_triangle, plus, square, square_cross,

    square_dot, square_pin, square_x, star, star_dot, triangle,

    triangle_dot, triangle_pin, x, y

这也是来自 Scatter 文档的标记类型图片:

© www.soinside.com 2019 - 2024. All rights reserved.