igraph / python-igraph

Python interface for igraph
GNU General Public License v2.0
1.3k stars 249 forks source link

Installing igraph tutorial from website: AttributeError: 'AxesSubplot' object has no attribute 'arc' #571

Closed lambo4jos closed 2 years ago

lambo4jos commented 2 years ago

I have python 3.8.13, igraph 0.10.1, with matplotlib 3.6.0, and when running the sample scripts from the install page on the website, I am receiving an error. The scripts can be found here: https://igraph.org/python/tutorial/latest/install.html

I am referring to the following

import matplotlib.pyplot as plt import igraph as ig fig, ax = plt.subplots() g = ig.Graph.Famous("petersen") ig.plot(g, target=ax) plt.show()

I receive the following errors at the ig.plot(g, target=ax) line:


AttributeError Traceback (most recent call last) Cell In [120], line 6 4 fig, ax = plt.subplots() 5 g = ig.Graph.Famous("petersen") ----> 6 ig.plot(g, target=ax) 8 plt.show()

File ~/anaconda3/envs/rapids-2208/lib/python3.8/site-packages/igraph/drawing/init.py:245, in plot(obj, target, bbox, *args, *kwds) 243 return 244 else: --> 245 plotter( 246 backend, 247 target, 248 palette=palette, 249 args, 250 **kwds, 251 ) 252 return target 254 # Cairo backend

File ~/anaconda3/envs/rapids-2208/lib/python3.8/site-packages/igraph/drawing/graph.py:558, in plot(self, backend, context, *args, *kwds) 552 from igraph.drawing import DrawerDirectory 554 drawer = kwds.pop( 555 "drawer_factory", 556 DrawerDirectory.resolve(self, backend)(context), 557 ) --> 558 drawer.draw(self, args, **kwds)

File ~/anaconda3/envs/rapids-2208/lib/python3.8/site-packages/igraph/drawing/matplotlib/graph.py:233, in MatplotlibGraphDrawer.draw(self, graph, *args, **kwds) 231 drawer_method = vertex_drawer.draw 232 for vertex, visual_vertex, coords in vertex_coord_iter: --> 233 drawer_method(visual_vertex, vertex, coords) 235 # Construct the iterator that we will use to draw the vertex labels 236 vs = graph.vs

File ~/anaconda3/envs/rapids-2208/lib/python3.8/site-packages/igraph/drawing/matplotlib/vertex.py:60, in MatplotlibVertexDrawer.draw(self, visual_vertex, vertex, coords) 49 width = ( 50 visual_vertex.width 51 if visual_vertex.width is not None 52 else visual_vertex.size 53 ) 54 height = ( 55 visual_vertex.height 56 if visual_vertex.height is not None 57 else visual_vertex.size 58 ) ---> 60 stroke = visual_vertex.shape.draw_path( 61 ax, 62 coords[0], 63 coords[1], 64 width, 65 height, 66 facecolor=visual_vertex.color, 67 edgecolor=visual_vertex.frame_color, 68 linewidth=visual_vertex.frame_width, 69 zorder=visual_vertex.zorder, 70 ) 71 ax.add_patch(stroke)

File ~/anaconda3/envs/rapids-2208/lib/python3.8/site-packages/igraph/drawing/shapes.py:168, in CircleDrawer.draw_path(ctx, center_x, center_y, width, height, kwargs) 166 return mpl.patches.Circle((center_x, center_y), width / 2, kwargs) 167 else: --> 168 ctx.arc(center_x, center_y, width / 2, 0, 2 * pi)

AttributeError: 'AxesSubplot' object has no attribute 'arc'


Not sure if its my python or package versions, or if its a configuration issue, or possibly an issue with igraph or matplotlib. Any advice would be most welcome

iosonofabio commented 2 years ago

Thank you for reporting. Let me take a look later today and I'll get back to you.

ntamas commented 2 years ago

Could this be related to API changes between Matplotlib 3.5.x and 3.6.0? I have noticed that several test cases also broke when the CI system updated Matplotlib 3.5.x to 3.6.0; this might be another instance of incompatibility between the two versions. @lambo4jos Can you try downgrading to Matplotlib 3.5.x to see if that resolves the problem?

Edit: nope, it actually looks like CircleDrawer does not recognize ctx as being a Matplotlib drawing context and tries to use it as a Cairo drawing context. We might have to update the code that tests for Matplotlib.

lambo4jos commented 2 years ago

@ntamas Down grading to Matplotlib 3.5.3 solved the issue encountered and the code now works as expected and the graphs are drawn as well.

@iosonofabio seems to be Matplotlib 3.6.0 is the culprit

iosonofabio commented 2 years ago

I just met Thomas Caswell yesterday, bad timing 😅

Jokes apart, I can just put in an if/else

iosonofabio commented 2 years ago

Just made a fresh install using igraph 0.10.1 and matplotlib 3.6.0 and cannot reproduce the bug. My code:

$ python -m venv .venv
$ .venv/bin/pip install igraph matplotlib==3.6.0
$ .venv/bin/python

and then in the Python interpreter:

import matplotlib.pyplot as plt
import igraph as ig
fig, ax = plt.subplots()
g = ig.Graph.Famous("petersen")
ig.plot(g, target=ax)
plt.show()

and it works like a charm:

igraph_mpl_issue

@lambo4jos are you sure matplotlib 3.6.0 was installed correctly in your environment? There's no change in mpl 3.6.0 that is related to anything we touch...

lambo4jos commented 2 years ago

@iosonofabio conda must have not installed the package correctly, as I upgraded back to 3.6.0 and things are now working as expected. this is really weird behavior

iosonofabio commented 2 years ago

No problem at all 😊 I'll close the issue, please reopen if that's not ok