python - 带有networkx的超图

标签 python nodes networkx hypergraph

有人熟悉networkx吗?

我尝试获取一个超图,我想将超边设置为列表中的其他彩色节点(大小取决于它们的值)。我想将节点设置为其他列表之外。 networkx 网站上的文档和示例确实很少,但我确信这是可能的。

我从 this example 开始,但这只是不同边缘的示例。有人可以告诉我如何在 networkx 中放置两组具有不同颜色/大小的不同节点吗?

最佳答案

您是否想要使用 nx.draw_networkx_nodes 使用 nodelist 参数进行类似的操作:

# Author: Aric Hagberg (hagberg@lanl.gov)
import matplotlib.pyplot as plt
import networkx as nx

G = nx.Graph()

G.add_edge('a', 'b', weight=0.6)
G.add_edge('a', 'c', weight=0.2)
G.add_edge('c', 'd', weight=0.1)
G.add_edge('c', 'e', weight=0.7)
G.add_edge('c', 'f', weight=0.9)
G.add_edge('a', 'd', weight=0.3)

elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 0.5]
esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] <= 0.5]

pos = nx.spring_layout(G)  # positions for all nodes

# nodes
nx.draw_networkx_nodes(G, pos, node_size=700, nodelist=['e','f','b'])
nx.draw_networkx_nodes(G, pos, node_size=1400, nodelist=['c','a','d'], node_color='blue')

# edges
nx.draw_networkx_edges(G, pos, edgelist=elarge,
                       width=6)
nx.draw_networkx_edges(G, pos, edgelist=esmall,
                       width=6, alpha=0.5, edge_color='b', style='dashed')

# labels
nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif')

plt.axis('off')
plt.show()

输出:

enter image description here

也改变标签和形状:

# Author: Aric Hagberg (hagberg@lanl.gov)
import matplotlib.pyplot as plt
import networkx as nx

G = nx.Graph()

G.add_edge('a', 'b', weight=0.6)
G.add_edge('a', 'c', weight=0.2)
G.add_edge('c', 'd', weight=0.1)
G.add_edge('c', 'e', weight=0.7)
G.add_edge('c', 'f', weight=0.9)
G.add_edge('a', 'd', weight=0.3)

elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 0.5]
esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] <= 0.5]

pos = nx.spring_layout(G)  # positions for all nodes

# nodes
nx.draw_networkx_nodes(G, pos, node_size=700, nodelist=['e','f','b'])
nx.draw_networkx_nodes(G, pos, node_size=1400, nodelist=['c','a','d'], 
                       node_color='blue', node_shape='^')

# edges
nx.draw_networkx_edges(G, pos, edgelist=elarge,
                       width=6)
nx.draw_networkx_edges(G, pos, edgelist=esmall,
                       width=6, alpha=0.5, edge_color='b', style='dashed')

# labels
nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif', 
                        labels={'f':'f','e':'e','b':'b'})
nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif', font_color='white',
                        labels={'d':'d','a':'a','c':'c'})

plt.axis('off')
plt.show()

输出:

enter image description here

关于python - 带有networkx的超图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682289/

相关文章:

python - 使用wxPython绘图,填充所有像素的速度有多快?

python - 按日期填充 Pandas 中的行,条件是行不存在

swift - 即使父节点正在移动,如何为每个节点在屏幕上提供相同的起始位置?

python - python中顶点列表(不是两个顶点)之间的距离

python - 查找给定节点的最高权重边

python - 使用 networkx 在两个节点之间绘制多条边

python - 如何在 pdb 中调试手动键入的表达式和语句?

python - 如何在 groupby.agg() 函数内组合 ';' .join 和 lambda x : x. tolist() ?

python - 在 networkx 中着色步行边缘

tree - ANTLR3 Hetero 节点未创建