【问题标题】:ASCII Visualisation of a graph of nodes in pythonpython中节点图的ASCII可视化
【发布时间】:2011-07-21 11:44:10
【问题描述】:

我有一个名为 Node 的类

class Node:
   def __init__(self,name, childList, parentList):
      self.name = name
      # a list of all nodes which are children of this node
      # may have length 0 to many
      self.childList = childList 
      # a list of all nodes which are parents of this node
      # may have length 0 to many
      self.parentList = parentList

我有一个节点列表(nodeList)。这些节点可能在彼此的父列表或子列表中。我希望能够将其 childLists 和 parentlists 中规定的节点之间的关系可视化到标准输出(作为 ASCII 绘图)。

例如,下面的名称是 nodeList 中节点的名称。

                           Classifier
                                |
                                |
                         FeatureCombiner
                          /           \
                         /             \
                        /               \
               FeatureGenerator1     FeatureGenerator2
                      \                     /
                       \                   /
                        \                 /
                         \               /
                          \             /
                           \           /
                            \         /
                            Image Loader

Classifier 有一个空的 parentList 和一个包含 FeatureCombiner 的长度为 1 的 childList。 FeatureGenerator1 和 2 具有相同的 parent 和 childList,分别包含 FeatureCombiner 和 Image Loader。 Image Loader 有一个空的 childList 和一个包含 FeatureGenerator1 和 2 的 parentList。

提前谢谢你, 马特

【问题讨论】:

    标签: python graph ascii visualization


    【解决方案1】:

    我们在DVC project 中遇到了类似的问题,在潜伏甚至尝试将Graph::Easy 移植到Python 之后,我们意识到没有跨平台的python 库可以做到这一点并且不需要一些重度依赖像 Graphviz。所以我们最终使用了一个名为 Grandalf 的神奇库来为我们处理布局,然后我们自己用 ASCII 呈现结果并使用寻呼机呈现它们(这就是像 git log 这样的东西使它的输出很好并且可水平和垂直滚动)。 See the whole code here.

    +-------------------+           +--------------------+
    | test_data.csv.dvc |           | train_data.csv.dvc |
    +-------------------+           +--------------------+
                      **              **                  
                        ***        ***                    
                           **    **                       
                    +-------------------+                 
                    | featurization.dvc |                 
                    +-------------------+                 
                      ***            ***                  
                    **                  ***               
                  **                       **             
        +--------------+                     **           
        | training.dvc |                   **             
        +--------------+                ***               
                      ***            ***                  
                         **        **                     
                           **    **                       
                         +---------+                      
                         | Dvcfile |                      
                         +---------+                      
    

    【讨论】:

    • 这真的很棒:)
    【解决方案2】:

    也许从 Perl 的 Graph::Easy 移植 ASCII 图形布局逻辑?

    【讨论】:

      【解决方案3】:

      在 ascii 中执行此操作并非易事,因为以下方面缺乏完整的答案就证明了这一点:

      Python ASCII Graph Drawing

      也就是说,有很多工具可用于以非 ascii 方式绘制图形。为初学者查看与 NetworkX 和 Matplotlib 相关的绘图功能:

      http://networkx.lanl.gov/

      http://matplotlib.sourceforge.net/

      还有pydot:

      http://code.google.com/p/pydot/

      【讨论】:

      • 谢谢,这些看起来可以以图像格式执行此操作。我只考虑二进制,因为我认为它会更容易
      猜你喜欢
      • 2019-11-30
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2015-06-06
      • 2014-03-13
      • 1970-01-01
      • 2012-11-26
      相关资源
      最近更新 更多