0

我有以下代码可以使用 ipycytoscape 生成和绘制图形:

from ipycytoscape import CytoscapeWidget
import networkx as nx
cyto = CytoscapeWidget()
nodes = [1,2,3,4,5,6,7,8]
children = [[2],[3,4],[5,6],[6,7],[8],[],[8],[]]
G2 = nx.Graph()
G2.add_nodes_from(nodes)
for i,child in enumerate(children):
    for c in child:
        G2.add_edge(i+1, c)

cyto.graph.add_graph_from_networkx(G2, directed=True)
cyto.set_layout(name='dagre', nodeSpacing=10, edgeLengthVal=10)
display(cyto)

到目前为止,一切都很好: 在此处输入图像描述

现在如果我添加一个布局,不管它是什么

cyto_style = [{ 'selector' : 'node',
                'style': {'font-family': 'arial',
                         'font-size': '10px',
                         'label': 'data(id)'}}]
cyto.set_style(cyto_style)
display(cyto)

在此处输入图像描述

箭头消失了!!!没有方向性绘图。我应该怎么做才能使箭头在添加样式之前保持原样?

4

1 回答 1

1

发生这种情况是因为 cytoscape.js(ipycytoscape 从中提取的库)需要具有以下样式配置才能显示箭头:

            {
                "selector": "edge.directed",
                "style": {
                    "curve-style": "bezier",
                    "target-arrow-shape": "triangle",
                    "target-arrow-color": "#9dbaea",
                },
            },

(当然你可以改变箭头的形状和颜色,但是必须指定这三个属性才能看到它们)。

这里的问题是,由于 traitlets 实现(Jupyter 实验室依赖于将后端与前端属性同步的库)不提供对列表或字典等深层对象的支持。换句话说,它只是自动同步简单的对象,如整数和字符串。幸运的是,我能够使用名为 spectate 的库在 ipycytoscape 中解决这个问题。但是,spectate 似乎无法同步嵌套的深层对象,也就是我们在创建styles.

我今天花了一段时间看它,但无法真正弄清楚如何解决它。我明天再试试。请考虑关注这个问题,因为我在堆栈溢出中不活跃。另外,如果您愿意尝试修复它,请告诉我。我是来回答问题和讨论想法的。干杯。

于 2021-07-26T15:59:54.660 回答