简体   繁体   English

如何使用Python创建变体树?

[英]How can I create a variant tree using Python?

I want to assess how many variants of a given number of features and corresponding attributes exist and then plot the combinations as a tree of branches, where each branch represents one combination/variant. 我想评估给定数量的特征和相应属性存在多少个变体,然后将组合绘制成一棵分支树,其中每个分支代表一个组合/变量。

Example: features: colors,size,dim. 示例: 特征:颜色,大小,暗淡。 Each feature has different attributes (colors: red,green,blue; size: big, small). 每个功能都有不同的属性 (颜色:红色,绿色,蓝色;大小:大,小)。

Through permutation I find the number of variants. 通过排列,我发现了变体的数量。 Each combination/variant is a branch of my variant tree. 每个组合/变体都是我的变体树的一个分支。 Eg ('red', 'big', 1) is one branch, ('red', 'big', 2) is another and so on. 例如('red','big',1)是一个分支,('red','big',2)是另一个分支,依此类推。

Is there any libary which can help me draw out these branches with nodes and arcs? 是否有任何库可以帮助我绘制带有节点和弧线的这些分支?

Code for permutations: 排列代码:

colors = ['red', 'green', 'blue']
size = ['big','small']
dim = [1,2,3]

from itertools import product

x =list (product(colors,size,dim))

print (x)
print ("Number of variants:",len(x))

[('red', 'big', 1), ('red', 'big', 2), ('red', 'big', 3), 
 ('red', 'small', 1), ('red', 'small', 2), ('red', 'small', 3), 
 ('green', 'big', 1), ('green', 'big', 2), ('green', 'big', 3), 
 ('green', 'small', 1), ('green', 'small', 2), ('green', 'small', 3), 
 ('blue', 'big', 1), ('blue', 'big', 2), ('blue', 'big', 3), 
 ('blue', 'small', 1), ('blue', 'small', 2), ('blue', 'small', 3)]

enter image description here 在此处输入图片说明

I was able to create this using the general graph description format DOT . 我能够使用常规图形描述格式DOT创建此文件 Python file main.py creating the DOT file: 创建DOT文件的Python文件main.py

from itertools import product

colors = ['red', 'green', 'blue']
size = ['big','small']
dim = [1,2,3]

print('digraph G {\n    rankdir=LR\n    node [shape=box]\n')
print('    start [label=""]')

for i, c in enumerate(colors):
    print(f'    color_{i} [label="{c}"]; start -> color_{i}')

    for j, s in enumerate(size):
        print(f'    size_{i}_{j} [label="{s}"]; color_{i} -> size_{i}_{j}')

        for k, d in enumerate(dim):
            print(f'    dim_{i}_{j}_{k} [label="{d}"]; size_{i}_{j} -> dim_{i}_{j}_{k}')
print('}')

I created the image using the following commands: 我使用以下命令创建了图像:

$ python main.py > graph.dot
$ dot -Tpng graph.dot -o graph.png

The dot command I used is from the graphviz package. 我使用的dot命令来自graphviz软件包。 I haven't used DOT much so I wasn't able to add the text and blue color. 我没有使用太多DOT,因此无法添加文本和蓝色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM