networkx:一致绘制图形

2024-09-26 21:56:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用networkx绘制3D立方体、4D超立方体和5D超立方体。在

如何控制draw()函数的工作方式?我希望他们都出来在完全相同的角度,大小和标签。在

cnt = 0
for uniqHC in nx_hcl:
    plt.figure()
    nx.draw(uniqHC)
    plt.savefig("uniqHC_" + str(cnt))
    plt.close()
    #nx.relabel_nodes(uniqHC, flatten)
    cnt += 1

如你所见,第一个还不错,第二个不是我想要的。很难理解和比较正在发生的事情。这只是三维的

enter image description here

enter image description here

这个软件可以绘制超立方体,它们看起来很棒。我希望我的图表以同样的风格绘制。下面是我想要的3D手机壳的图片。我只标注“000”、“001”就可以了。我试过聪明点,从它们的超立方体开始,去掉有向边,再加上我的方向,但它随机地扭曲了图形的形状,看起来和以前一样!在

^{pr2}$

enter image description here


Tags: 函数innetworkxfor方式绘制plt标签
1条回答
网友
1楼 · 发布于 2024-09-26 21:56:00

在networkx中使用spring布局方法可能无法获得所需的结果(一致的方向和间距)。这是默认值,也是调用draw()时发生的情况。在

如果您知道要将节点定位到何处,可以像这样指定

In [1]: import networkx as nx

In [2]: G = nx.hypercube_graph(2)

In [3]: pos = nx.spring_layout(G)

In [4]: pos
Out[4]: 
{(0, 0): array([ 0.67137792,  0.44182572]),
 (0, 1): array([ 0.42261686,  1.        ]),
 (1, 0): array([ 0.24869904,  0.        ]),
 (1, 1): array([ 0.        ,  0.55858863])}

In [5]: pos[(0,0)]=(0,0)

In [6]: pos[(0,1)]=(0,1)

In [7]: pos[(1,0)]=(1,0)

In [8]: pos[(1,1)]=(1,1)

In [9]: nx.draw(G,pos)

enter image description here

相关问题 更多 >

    热门问题