尝试使用 python-igraph 绘制图形时,关键错误属性不存在

key error attribute does not exist when trying to plot graph with python-igraph

我正在尝试学习本教程:https://towardsdatascience.com/newbies-guide-to-python-igraph-4e51689c35b4

我严格按照它来操作(真的是复制粘贴),但我不断收到以下错误:

KeyError: 'Attribute does not exist

谁能帮我弄清楚原因。

(我是 运行 python 3.10 并使用 python-igraph 0.9.9)

这是正在执行的代码:

from igraph import *

# Create graph
g = Graph(directed=True)

# Add 5 vertices
g.add_vertices(5)

# Add ids and labels to vertices
for i in range(len(g.vs)):
    g.vs[i]["id"]= i
    g.vs[i]["label"]= str(i)

# Add edges
g.add_edges([(0,2),(0,1),(0,3),(1,2),(1,3),(2,4),(3,4)])

# Add weights and edge labels
weights = [8,6,3,5,6,4,9]
g.es['weight'] = weights
g.es['label'] = weights

visual_style = {}

out_name = "graph.png"

# Set bbox and margin
visual_style["bbox"] = (400,400)
visual_style["margin"] = 27

# Set vertex colours
visual_style["vertex_color"] = 'white'

# Set vertex size
visual_style["vertex_size"] = 45

# Set vertex lable size
visual_style["vertex_label_size"] = 22

# Don't curve the edges
visual_style["edge_curved"] = False

# Set the layout
my_layout = g.layout_lgl()
visual_style["layout"] = my_layout

# Plot the graph
plot(g, out_name, **visual_style)

完整跟踪如下:

KeyError                                  Traceback (most recent call last)
/var/folders/n3/l9gl3dx13n5bywlfvjm7hpv80000gq/T/ipykernel_11555/3946746501.py in <module>
     24 
     25 # Plot the graph
---> 26 plot(g, out_name, **visual_style)

~/.pyenv/versions/3.10.0/lib/python3.10/site-packages/igraph/drawing/__init__.py in plot(obj, target, bbox, *args, **kwds)
    510         result.show()
    511     elif isinstance(target, str):
--> 512         result.save()
    513 
    514     # Also return the plot itself

~/.pyenv/versions/3.10.0/lib/python3.10/site-packages/igraph/drawing/__init__.py in save(self, fname)
    290         """
    291         if self._is_dirty:
--> 292             self.redraw()
    293         if isinstance(self._surface, cairo.ImageSurface):
    294             if fname is None and self._need_tmpfile:

~/.pyenv/versions/3.10.0/lib/python3.10/site-packages/igraph/drawing/__init__.py in redraw(self, context)
    274                 else:
    275                     ctx.save()
--> 276                 plotter(ctx, bbox, palette, *args, **kwds)
    277                 if opacity < 1.0:
    278                     ctx.pop_group_to_source()

~/.pyenv/versions/3.10.0/lib/python3.10/site-packages/igraph/__init__.py in __plot__(self, context, bbox, palette, *args, **kwds)
   4164             del kwds["drawer_factory"]
   4165         drawer = drawer_factory(context, bbox)
-> 4166         drawer.draw(self, palette, *args, **kwds)
   4167 
   4168     def __str__(self):

~/.pyenv/versions/3.10.0/lib/python3.10/site-packages/igraph/drawing/graph.py in draw(self, graph, palette, *args, **kwds)
    487             src, dest = edge.tuple
    488             src_vertex, dest_vertex = vertex_builder[src], vertex_builder[dest]
--> 489             (x, y), (halign, valign) = edge_drawer.get_label_position(
    490                 edge, src_vertex, dest_vertex
    491             )

~/.pyenv/versions/3.10.0/lib/python3.10/site-packages/igraph/drawing/edge.py in get_label_position(self, edge, src_vertex, dest_vertex)
    157 
    158         # Determine the midpoint
--> 159         if edge['curved']:
    160             (x1, y1), (x2, y2) = src_vertex.position, dest_vertex.position
    161             aux1, aux2 = get_bezier_control_points_for_curved_edge(x1, y1, x2, y2, edge['curved'])

KeyError: 'Attribute does not exist'

通过源代码和您的回溯挖掘,this line 似乎很关键。尝试在设置其他 edge-style 属性的位置设置 g.es["curved"] = Trueg.es["curved"] = False

我维护了igraph的绘图界面

master 或任何版本 <=0.9.x 相比,当前 develop 分支中的绘图支持有了显着改进。

这是其中一个错误,还有更多错误,但是因为整个设计已经改变,向后移植是一场噩梦,而且很可能不会发生。

所以我建议您现在是否可以找到解决方法(例如,参见@Sarah 的回答),否则从源安装 develop 分支或等到 0.10,具体取决于你的技能和紧迫感。抱歉。

编辑:我会在之前的答案中添加评论,并澄清 github 问题已用相同的答案关闭。不幸的是,Whosebug 认为我不值得发表评论 ;-)