【问题标题】:Drawing network with nodes and edges in python在python中使用节点和边绘制网络
【发布时间】:2018-08-25 10:10:38
【问题描述】:
g=nx.DiGraph(directed=True)
g.add_nodes_from(o)
for j in range (len(o)):
   for i in range(len(ixx)):
      g.add_edge(ixx[i],o[j-1])
   g.add_edge(o[j-1],WIN[0], weight=10)   
nx.draw(g,with_labels=True)
plt.draw()
plt.show()

这是图表的代码。 ixx 是输入节点,WIN 是单个输出节点,o 是隐藏节点。

示例网络如下所示。 (这是我运行代码时得到的) Numbers from 1 to ...26 are hidden nodes. 27 is output node

但是,我想把它画成:左侧的输入节点,隐藏在中间,右侧的输出节点。就像神经网络的外观一样。

【问题讨论】:

  • 你能告诉我们你的尝试吗?
  • 你应该看看pos 参数。另外,this previous answer of mine 可能会有所帮助。
  • 这不是“为我编写代码”论坛。如果您遇到了您研究过的特定问题(google/SO)并且无法自行解决,您必须加紧向我们提供您的代码。阅读 how-to-askon topic ,提供符合 How to create a Minimal, Complete, and Verifiable example 的代码以及您的代码无法满足的异常/期望,我相信 SO 会帮助您。根据您的规范进行编码不适合 SO Q&A 格式。

标签: python-3.x networkx


【解决方案1】:

这里有一个演示如何做到这一点:

import networkx as nx
import matplotlib.pyplot as plt
from random import sample, seed 

seed(0)
G=nx.dodecahedral_graph()

# splitting the graph to the sets of nodes
samples = sample(G.nodes,2)
output_set = set(samples[:1])
input_set = set(samples[1:2])
hidden_set = set(G.nodes) - output_set - input_set

# a function for shifting node positions
def make_spring_pos(nodes,shift):
  return { node:(pos[0]+shift,pos[1]) for node,pos in nx.spring_layout(G.subgraph(nodes)).items() }

# shifting node positions for different sets
input_pos= make_spring_pos(input_set, shift=0)
hidden_pos= make_spring_pos(hidden_set, shift=3)
output_pos= make_spring_pos(output_set, shift=6)

# merging positions back
all_pos = {}
for pos in [input_pos,hidden_pos,output_pos]:
  all_pos.update(pos)

nx.draw_networkx(G, pos=all_pos)

plt.show()

【讨论】:

  • 谢谢你,这很有用。
猜你喜欢
  • 1970-01-01
  • 2018-05-10
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 2012-10-03
  • 1970-01-01
相关资源
最近更新 更多