难以在 graphviz python 包中分离行列
Difficulty separating ranks in graphviz python package
我正在尝试使用 graphviz Python 包创建神经网络图,并且我 运行 遇到了试图防止等级重叠等问题。我特别想问一下如何防止 post.
中的排名重叠
这是我目前的代码,
from graphviz import Graph
d = Graph(name="Network Graph",graph_attr={'rankdir':'TD'},engine='dot')
with d.subgraph() as s:
s.attr(rank='same')
s.node('density','density')
s.node('te','Elec. Temp.')
s.node('tr','Rad. Temp.')
s.node('alpha','alpha')
layer0 = ['density','te','tr','alpha']
layer1 = []
layer2 = []
layer3 = []
layer4 = []
layer5 = []
allLayers =[ layer0, layer1, layer2, layer3, layer4, layer5 ]
with d.subgraph() as s1:
s1.attr(rank='same')
for i in range(1,7):
layer1.append('a1%i'%i)
s1.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph() as s2:
s2.attr(rank='same')
for i in range(1,10):
layer2.append('a1%i'%i)
s2.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph() as s3:
s3.attr(rank='same')
for i in range(1,3):
layer3.append('a1%i'%i)
s3.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph() as s4:
s4.attr(rank='same')
for i in range(1,7):
layer4.append('a1%i'%i)
s4.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph() as s5:
s5.attr(rank='same')
for i in range(1,14):
layer5.append('a1%i'%i)
s5.node('a1%i'%i,'',tailclip='true',headclip='true')
edges = []
d.attr(rankdir="LR",splines='line',ranksep='2.0',overlap='false')
for n in range(0,len(allLayers)-1):
# d.attr(rankdir="LR",splines='line',ranksep='2.0',overlap='false')
for i in allLayers[n]:
for j in allLayers[n+1]:
# edges.append((i,j))
d.edge(i,j,directed="False")
print(d.source)
d.render('network.pdf',view=True)
这产生了这个图像
我不知道为什么我能够将 ranks/layers 0 和 1 分开,但随后所有后续层都堆叠在第 1 层之上。出于某种原因,d.attr(rankdir="LR",splines='line',ranksep='2.0',overlap='false')
行似乎只应用一次,但是当我将该行放在 for
循环中时,它的行为相同。
graphviz 包的文档还有很多不足之处,而且我在互联网上找到的 Python Graphviz 的帮助也很少。此外,显然不能在创建 Graph()
对象后立即放置 d.attr(rankdir='LR')
。它必须在创建所有节点之后放置,因此顺序显然很重要。
仅供参考:我还尝试在不同的地方添加参数 overlap='false'
,但这并没有改变我使用它的方式的行为。
如何区分行列?使用 Graphviz 时是否还有其他一般提示,特别是在如何正确排序命令和设置属性方面?
我发现的问题是由于 copy-pasta,我未能更改节点名称。我以前在每一层的生成中都有 a1%i'%i
作为标签。当然,解决方法是将每个后续层中的 1
更改为与层号对应的整数。
很高兴看到您解决了问题。但您可能会发现以下更改很有帮助。看看他们。
- 为每个子图命名
- 为您的节点使用不同的名称(您已经意识到)
- 改用有向图
- 您实际上可以在实例化时设置所有图形属性
from graphviz import Digraph
d = Digraph(name="Network Graph",
graph_attr={'rankdir':'LR', 'splines':'line','ranksep':'2.0','overlap':'false'},
engine='dot')
with d.subgraph(name='s') as s:
s.node('density','density')
s.node('te','Elec. Temp.')
s.node('tr','Rad. Temp.')
s.node('alpha','alpha')
layer0 = ['density','te','tr','alpha']
layer1 = []
layer2 = []
layer3 = []
layer4 = []
layer5 = []
allLayers =[ layer0, layer1, layer2, layer3, layer4, layer5 ]
with d.subgraph(name='s1') as s1:
s1.attr(rank='same')
for i in range(1,7):
layer1.append('a1%i'%i)
s1.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph(name='s2') as s2:
s2.attr(rank='same')
for i in range(1,10):
layer2.append('a2%i'%i)
s2.node('a2%i'%i,'',tailclip='true',headclip='true')
with d.subgraph(name='s3') as s3:
s3.attr(rank='same')
for i in range(1,3):
layer3.append('a3%i'%i)
s3.node('a3%i'%i,'',tailclip='true',headclip='true')
with d.subgraph(name='s4') as s4:
s4.attr(rank='same')
for i in range(1,7):
layer4.append('a4%i'%i)
s4.node('a4%i'%i,'',tailclip='true',headclip='true')
with d.subgraph(name='s5') as s5:
s5.attr(rank='same')
for i in range(1,14):
layer5.append('a5%i'%i)
s5.node('a5%i'%i,'',tailclip='true',headclip='true')
for n in range(0,len(allLayers)-1):
for i in allLayers[n]:
for j in allLayers[n+1]:
d.edge(i,j,directed="False")
# print(d.source)
# d.render('network.pdf',view=True)
您可能还会发现以下链接有帮助
我正在尝试使用 graphviz Python 包创建神经网络图,并且我 运行 遇到了试图防止等级重叠等问题。我特别想问一下如何防止 post.
中的排名重叠这是我目前的代码,
from graphviz import Graph
d = Graph(name="Network Graph",graph_attr={'rankdir':'TD'},engine='dot')
with d.subgraph() as s:
s.attr(rank='same')
s.node('density','density')
s.node('te','Elec. Temp.')
s.node('tr','Rad. Temp.')
s.node('alpha','alpha')
layer0 = ['density','te','tr','alpha']
layer1 = []
layer2 = []
layer3 = []
layer4 = []
layer5 = []
allLayers =[ layer0, layer1, layer2, layer3, layer4, layer5 ]
with d.subgraph() as s1:
s1.attr(rank='same')
for i in range(1,7):
layer1.append('a1%i'%i)
s1.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph() as s2:
s2.attr(rank='same')
for i in range(1,10):
layer2.append('a1%i'%i)
s2.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph() as s3:
s3.attr(rank='same')
for i in range(1,3):
layer3.append('a1%i'%i)
s3.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph() as s4:
s4.attr(rank='same')
for i in range(1,7):
layer4.append('a1%i'%i)
s4.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph() as s5:
s5.attr(rank='same')
for i in range(1,14):
layer5.append('a1%i'%i)
s5.node('a1%i'%i,'',tailclip='true',headclip='true')
edges = []
d.attr(rankdir="LR",splines='line',ranksep='2.0',overlap='false')
for n in range(0,len(allLayers)-1):
# d.attr(rankdir="LR",splines='line',ranksep='2.0',overlap='false')
for i in allLayers[n]:
for j in allLayers[n+1]:
# edges.append((i,j))
d.edge(i,j,directed="False")
print(d.source)
d.render('network.pdf',view=True)
这产生了这个图像
我不知道为什么我能够将 ranks/layers 0 和 1 分开,但随后所有后续层都堆叠在第 1 层之上。出于某种原因,d.attr(rankdir="LR",splines='line',ranksep='2.0',overlap='false')
行似乎只应用一次,但是当我将该行放在 for
循环中时,它的行为相同。
graphviz 包的文档还有很多不足之处,而且我在互联网上找到的 Python Graphviz 的帮助也很少。此外,显然不能在创建 Graph()
对象后立即放置 d.attr(rankdir='LR')
。它必须在创建所有节点之后放置,因此顺序显然很重要。
仅供参考:我还尝试在不同的地方添加参数 overlap='false'
,但这并没有改变我使用它的方式的行为。
如何区分行列?使用 Graphviz 时是否还有其他一般提示,特别是在如何正确排序命令和设置属性方面?
我发现的问题是由于 copy-pasta,我未能更改节点名称。我以前在每一层的生成中都有 a1%i'%i
作为标签。当然,解决方法是将每个后续层中的 1
更改为与层号对应的整数。
很高兴看到您解决了问题。但您可能会发现以下更改很有帮助。看看他们。
- 为每个子图命名
- 为您的节点使用不同的名称(您已经意识到)
- 改用有向图
- 您实际上可以在实例化时设置所有图形属性
from graphviz import Digraph
d = Digraph(name="Network Graph",
graph_attr={'rankdir':'LR', 'splines':'line','ranksep':'2.0','overlap':'false'},
engine='dot')
with d.subgraph(name='s') as s:
s.node('density','density')
s.node('te','Elec. Temp.')
s.node('tr','Rad. Temp.')
s.node('alpha','alpha')
layer0 = ['density','te','tr','alpha']
layer1 = []
layer2 = []
layer3 = []
layer4 = []
layer5 = []
allLayers =[ layer0, layer1, layer2, layer3, layer4, layer5 ]
with d.subgraph(name='s1') as s1:
s1.attr(rank='same')
for i in range(1,7):
layer1.append('a1%i'%i)
s1.node('a1%i'%i,'',tailclip='true',headclip='true')
with d.subgraph(name='s2') as s2:
s2.attr(rank='same')
for i in range(1,10):
layer2.append('a2%i'%i)
s2.node('a2%i'%i,'',tailclip='true',headclip='true')
with d.subgraph(name='s3') as s3:
s3.attr(rank='same')
for i in range(1,3):
layer3.append('a3%i'%i)
s3.node('a3%i'%i,'',tailclip='true',headclip='true')
with d.subgraph(name='s4') as s4:
s4.attr(rank='same')
for i in range(1,7):
layer4.append('a4%i'%i)
s4.node('a4%i'%i,'',tailclip='true',headclip='true')
with d.subgraph(name='s5') as s5:
s5.attr(rank='same')
for i in range(1,14):
layer5.append('a5%i'%i)
s5.node('a5%i'%i,'',tailclip='true',headclip='true')
for n in range(0,len(allLayers)-1):
for i in allLayers[n]:
for j in allLayers[n+1]:
d.edge(i,j,directed="False")
# print(d.source)
# d.render('network.pdf',view=True)
您可能还会发现以下链接有帮助