• <small id='ObcoU'></small><noframes id='ObcoU'>

    <legend id='ObcoU'><style id='ObcoU'><dir id='ObcoU'><q id='ObcoU'></q></dir></style></legend>

      • <bdo id='ObcoU'></bdo><ul id='ObcoU'></ul>
    1. <i id='ObcoU'><tr id='ObcoU'><dt id='ObcoU'><q id='ObcoU'><span id='ObcoU'><b id='ObcoU'><form id='ObcoU'><ins id='ObcoU'></ins><ul id='ObcoU'></ul><sub id='ObcoU'></sub></form><legend id='ObcoU'></legend><bdo id='ObcoU'><pre id='ObcoU'><center id='ObcoU'></center></pre></bdo></b><th id='ObcoU'></th></span></q></dt></tr></i><div id='ObcoU'><tfoot id='ObcoU'></tfoot><dl id='ObcoU'><fieldset id='ObcoU'></fieldset></dl></div>

      <tfoot id='ObcoU'></tfoot>
      1. 使用 grid_2d_graph 在 networkx 中绘制 MxM 节点的方形网格时移除旋转效果

        时间:2023-07-03
          <tbody id='JTNZo'></tbody>
        <legend id='JTNZo'><style id='JTNZo'><dir id='JTNZo'><q id='JTNZo'></q></dir></style></legend>

          • <bdo id='JTNZo'></bdo><ul id='JTNZo'></ul>
              • <small id='JTNZo'></small><noframes id='JTNZo'>

                <tfoot id='JTNZo'></tfoot>
              • <i id='JTNZo'><tr id='JTNZo'><dt id='JTNZo'><q id='JTNZo'><span id='JTNZo'><b id='JTNZo'><form id='JTNZo'><ins id='JTNZo'></ins><ul id='JTNZo'></ul><sub id='JTNZo'></sub></form><legend id='JTNZo'></legend><bdo id='JTNZo'><pre id='JTNZo'><center id='JTNZo'></center></pre></bdo></b><th id='JTNZo'></th></span></q></dt></tr></i><div id='JTNZo'><tfoot id='JTNZo'></tfoot><dl id='JTNZo'><fieldset id='JTNZo'></fieldset></dl></div>

                  本文介绍了使用 grid_2d_graph 在 networkx 中绘制 MxM 节点的方形网格时移除旋转效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要生成一个具有 100x100 节点的常规图(也称为格网络).我开始使用以下代码绘制 10x10 图形:

                  导入numpy从 numpy 导入 *将 networkx 导入为 nx从networkx导入*将 matplotlib.pyplot 导入为 pltG=nx.grid_2d_graph(10,10)nx.draw(G)plt.axis('关闭')plt.show()

                  但我得到的是:

                  有什么方法可以消除输出的这种旋转效应吗?我的最终网络必须看起来像一个国际象棋桌,就像这样(请忽略标签):

                  另外,我需要为每个节点提供其 ID,范围从 0 到 9999(在 100x100 网络的情况下).任何想法将不胜感激!

                  解决方案

                  默认情况下,

                  编辑

                  使用@AbdallahSobehy 的建议,我们可以从左到右和从上到下标记节点.

                  labels = dict( ((i, j), i + (N-1-j) * 10 ) for i, j in G.nodes() )

                  I need to generate a regular graph (also known as lattice network) which has 100x100 nodes. I started off with drawing a 10x10 graph with the following code:

                  import numpy
                  from numpy import *
                  import networkx as nx
                  from networkx import *
                  import matplotlib.pyplot as plt
                  
                  G=nx.grid_2d_graph(10,10)        
                  nx.draw(G)
                  
                  plt.axis('off')
                  plt.show()
                  

                  but what I get is this:

                  Is there any way of getting rid of this sort of rotation effect the output has? My final network must look like a chess table, just like this (please ignore the lables):

                  Also, I need to give each node its ID, ranging from 0 to 9999 (in the case of the 100x100 network). Any idea will be much appreciated!

                  解决方案

                  By default, networkx.draw uses a spring layout. Instead, you can provide your own positions with parameter pos. This is actually really simple, since the labels of nodes given networkx.grid_2d_graph actually are a (row, column) tuple:

                  >>> G=nx.grid_2d_graph(2,2)
                  [(0, 1), (1, 0), (0, 0), (1, 1)]
                  

                  Thus you can use a node's name as its position. So you just need to create a dictionary mapping nodes to themselves, and pass that as the position.

                  pos = dict( (n, n) for n in G.nodes() )
                  

                  However, since you also want to add node labels, you should use networkx.draw_networkx, which takes a dictionary of custom labels as an optional parameter. You'll need a dictionary mapping nodes to their new labels. Since NetworkX gives each node the label (row, column) by default, we can just label each node with row * 10 + column:

                  labels = dict( ((i, j), i * 10 + j) for i, j in G.nodes() )
                  

                  Putting it all together, you get the following code which yields the graph below:

                  import networkx as nx
                  import matplotlib.pyplot as plt
                  
                  N = 10
                  G=nx.grid_2d_graph(N,N)
                  pos = dict( (n, n) for n in G.nodes() )
                  labels = dict( ((i, j), i * 10 + j) for i, j in G.nodes() )
                  nx.draw_networkx(G, pos=pos, labels=labels)
                  
                  plt.axis('off')
                  plt.show()
                  

                  EDIT

                  Using the suggestion from @AbdallahSobehy, we can label the nodes from left to right and top to bottom.

                  labels = dict( ((i, j), i + (N-1-j) * 10 ) for i, j in G.nodes() )
                  

                  这篇关于使用 grid_2d_graph 在 networkx 中绘制 MxM 节点的方形网格时移除旋转效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:图像旋转后如何重新映射点? 下一篇:如何找到对象(形状)的方向?- Python Opencv

                  相关文章

                  1. <tfoot id='hh5vR'></tfoot>

                    <i id='hh5vR'><tr id='hh5vR'><dt id='hh5vR'><q id='hh5vR'><span id='hh5vR'><b id='hh5vR'><form id='hh5vR'><ins id='hh5vR'></ins><ul id='hh5vR'></ul><sub id='hh5vR'></sub></form><legend id='hh5vR'></legend><bdo id='hh5vR'><pre id='hh5vR'><center id='hh5vR'></center></pre></bdo></b><th id='hh5vR'></th></span></q></dt></tr></i><div id='hh5vR'><tfoot id='hh5vR'></tfoot><dl id='hh5vR'><fieldset id='hh5vR'></fieldset></dl></div>

                    <small id='hh5vR'></small><noframes id='hh5vR'>

                        <bdo id='hh5vR'></bdo><ul id='hh5vR'></ul>
                    1. <legend id='hh5vR'><style id='hh5vR'><dir id='hh5vR'><q id='hh5vR'></q></dir></style></legend>