具有与其他图形类似的顶点定位的绘图图

问题描述 投票:0回答:2

我正在尝试绘制一些图形并且它是互补的,我希望在绘制它们时顶点位于相同的位置。例如,在绘制具有4个顶点的循环图时: 我希望能够绘制它的互补(边1-3和0-2),同时使顶点在相同的位置。相反,当绘制它的补充时,我得到这个:

我尝试使用字典作为位置但是,因为我正在绘制具有n个顶点的多个字典,所以这仅适用于特定情况。

plot graph sage
2个回答
1
投票

对于您的具体用例,我认为有一个很好的方法.complement()保留这个。

sage: H = graphs.CycleGraph(6)                        
sage: graphics_array([H.plot(),H.complement().plot()])

6 Cycle graph and its complement

但是,一般情况下,您可能希望在相同位置绘制另一个图形,并且

使用字典

确实是你想要做的。一个更简单的例子是你的第一个,但我调整了新的图表,使它不再是一个补充。

sage: G = graphs.CycleGraph(4)
sage: pos_dict = G.get_pos()
sage: pos_dict
{0: (0.0, 1.0), 1: (-1.0, 0.0), 2: (0.0, -1.0), 3: (1.0, 0.0)}

这是有问题的字典,一个位置字典。现在我制作新图表,并尝试用另一个图表绘制它。

sage: G1 = Graph({1: [3,2], 2: [0]})
sage: graphics_array([G.plot(),G1.plot(pos=pos_dict)])

two more graphs


0
投票

我为我的n顶点问题找到了一个有效的解决方案。这是代码:

n = 6

# Don't modify
posi = {}
r = 1

G = Graph()
F = Graph()

for i in [0..(n-1)]:
    for j in [0..(n-1)]:
        if i==(j+1)%n or j==(i+1)%n : 
            G.add_edge( (i ,j) )
F = G.complement()

for i in [0..(n-1)]:
    if n % 2 != 0 and i == 0:
        x=0.5
        y=i
    else:
        if i < n/2:
            x=-0.15 if i%2 !=0 else 0.15 #Avoid consecutive vertices to be in-line
            y=i
        else:
            x=0.85 if i%2 !=0 else 1.15  #Avoid consecutive vertices to be in-line
            y=i-r
            r = r+2
    posi[i] = [x, y]

结果(也将它们绘制在一起): qazxsw poi

在写这篇文章时,我认为有“冷静”的方法可以做到这一点,但这完美无缺。

© www.soinside.com 2019 - 2024. All rights reserved.