使用 ipycytoscape 进行网络定制
Network customization using ipycytoscape
我正在使用 ipycytoscape 构建网络,但我不清楚如何让节点大小取决于节点的度数并为每个节点添加标签。
我的网络有两列,一列用于节点,一列用于目标。
Data
Nodes Target
A B
B C
B D
C B
E nan
G = nx.from_pandas_edgelist(df, source='Nodes', target='Target')
目前我正在构建图表如下:
cytoscapeobj = ipycytoscape.CytoscapeWidget()
cytoscapeobj.graph.add_graph_from_networkx(G, labels) # labels however is not adding any label to the node, so probably the location is wrong
cytoscapeobj
我知道使用设置样式进行自定义:
cytoscapeobj.set_style(my_style)
但我不知道如何更改它以便根据节点的度可视化标签和节点的大小。
有人用过 ipycytoscape 吗?
为了能够可视化标签,您需要将节点属性添加到您的 networkx 图表并设置适当的样式。对于样式,您可以为所有节点定义一个以显示标签。要根据度数控制节点的大小,您需要将度数列添加到节点数据框中。您可以使用 networkx degree API 来完成此操作。您首先需要创建图表,然后根据 networkx degree API 重新创建节点数据框,并添加包含 degree 属性的节点属性,以便能够在考虑度信息的情况下呈现图表。
完整的解决方案如下:
import ipycytoscape as cy
import networkx as nx
import pandas as pd
edge_data = {
'source': ['A', 'B', 'B', 'C'],
'target': ['B', 'C', 'D', 'B'],
}
link_df = pd.DataFrame.from_dict(edge_data)
node_data = {
'id': ['A', 'B', 'C', 'D', 'E']
}
node_df = pd.DataFrame.from_dict(node_data)
G = nx.from_pandas_edgelist(link_df)
node_df = pd.DataFrame(G.degree(), columns=['id', 'degree'])
nx.set_node_attributes(G, node_df.set_index('id').to_dict('index'))
cytoscapeobj = cy.CytoscapeWidget()
cytoscapeobj.graph.add_graph_from_networkx(G)
cytoscapeobj.set_style(
[
{
'selector': 'node',
'style': {
'font-family': 'helvetica',
'font-size': '20px',
'label': 'data(id)'
}
},
{
'selector': 'edge',
'style': {
'font-family': 'helvetica',
'font-size': '20px'
}
},
{
'selector': 'node[degree>0]',
'style': {
'width': '100px',
'height': '100px'
}
},
{
'selector': 'node[degree>1]',
'style': {
'width': '150px',
'height': '150px'
}
},
{
'selector': 'node[degree>2]',
'style': {
'width': '200px',
'height': '200px'
}
}
]
)
cytoscapeobj
如果您想要相对值而不是绝对值,您可以在宽度和高度中使用 % 而不是 px。
我正在使用 ipycytoscape 构建网络,但我不清楚如何让节点大小取决于节点的度数并为每个节点添加标签。 我的网络有两列,一列用于节点,一列用于目标。
Data
Nodes Target
A B
B C
B D
C B
E nan
G = nx.from_pandas_edgelist(df, source='Nodes', target='Target')
目前我正在构建图表如下:
cytoscapeobj = ipycytoscape.CytoscapeWidget()
cytoscapeobj.graph.add_graph_from_networkx(G, labels) # labels however is not adding any label to the node, so probably the location is wrong
cytoscapeobj
我知道使用设置样式进行自定义:
cytoscapeobj.set_style(my_style)
但我不知道如何更改它以便根据节点的度可视化标签和节点的大小。 有人用过 ipycytoscape 吗?
为了能够可视化标签,您需要将节点属性添加到您的 networkx 图表并设置适当的样式。对于样式,您可以为所有节点定义一个以显示标签。要根据度数控制节点的大小,您需要将度数列添加到节点数据框中。您可以使用 networkx degree API 来完成此操作。您首先需要创建图表,然后根据 networkx degree API 重新创建节点数据框,并添加包含 degree 属性的节点属性,以便能够在考虑度信息的情况下呈现图表。
完整的解决方案如下:
import ipycytoscape as cy
import networkx as nx
import pandas as pd
edge_data = {
'source': ['A', 'B', 'B', 'C'],
'target': ['B', 'C', 'D', 'B'],
}
link_df = pd.DataFrame.from_dict(edge_data)
node_data = {
'id': ['A', 'B', 'C', 'D', 'E']
}
node_df = pd.DataFrame.from_dict(node_data)
G = nx.from_pandas_edgelist(link_df)
node_df = pd.DataFrame(G.degree(), columns=['id', 'degree'])
nx.set_node_attributes(G, node_df.set_index('id').to_dict('index'))
cytoscapeobj = cy.CytoscapeWidget()
cytoscapeobj.graph.add_graph_from_networkx(G)
cytoscapeobj.set_style(
[
{
'selector': 'node',
'style': {
'font-family': 'helvetica',
'font-size': '20px',
'label': 'data(id)'
}
},
{
'selector': 'edge',
'style': {
'font-family': 'helvetica',
'font-size': '20px'
}
},
{
'selector': 'node[degree>0]',
'style': {
'width': '100px',
'height': '100px'
}
},
{
'selector': 'node[degree>1]',
'style': {
'width': '150px',
'height': '150px'
}
},
{
'selector': 'node[degree>2]',
'style': {
'width': '200px',
'height': '200px'
}
}
]
)
cytoscapeobj
如果您想要相对值而不是绝对值,您可以在宽度和高度中使用 % 而不是 px。