Reputation: 4003
I'm trying to following this tutorial: https://towardsdatascience.com/newbies-guide-to-python-igraph-4e51689c35b4
I followed it to the letter (copy and paste really) and I keep getting the following error:
KeyError: 'Attribute does not exist
Can anyone help me figure out why.
(I'm running python 3.10 and using python-igraph 0.9.9)
Here's the code being executed:
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)
The full trace is below:
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'
Upvotes: 0
Views: 934
Reputation: 81
I maintain the plotting interface of igraph.
Plotting support has improved dramatically in the current develop
branch compared to master
or any release <=0.9.x
.
This is one of those bugs, there are many more, however because the entire design has changed backporting is a nightmare and most likely not happening.
So I'd recommend if you can find a workaround (e.g. see @Sarah's answer) for now do that, otherwise install the develop
branch from source or wait until 0.10
, depending on your skills and urgency. Sorry about that.
edit: I'd add a comment to the previous answer as well clarifying the github issue is closed with the same answer. Unfortunately, stackoverflow does not think I'm worthy of posting comments ;-)
Upvotes: 1
Reputation: 4023
Digging through the source code and your traceback, it seems like this line is critical. Try setting g.es["curved"] = True
or g.es["curved"] = False
up where you're setting the other edge-style properties.
Upvotes: 1