How to change thickness of individual edges (tutorial seems broken)

I am trying to learn how to plot different edges with different thickness in igraph. I have tried the tutorial example at Maximum Bipartite Matching but it fails. The code is:

import igraph as ig
import matplotlib.pyplot as plt

# Assign nodes 0-4 to one side, and the nodes 5-8 to the other side
g = ig.Graph.Bipartite(
    [0, 0, 0, 0, 0, 1, 1, 1, 1],
    [(0, 5), (1, 6), (1, 7), (2, 5), (2, 8), (3, 6), (4, 5), (4, 6)]
)
assert g.is_bipartite()

matching = g.maximum_bipartite_matching()
matching = g.maximum_bipartite_matching()

# Print pairings for each node on one side
matching_size = 0
print("Matching is:")
for i in range(5):
    print(f"{i} - {matching.match_of(i)}")
    if matching.is_matched(i):
        matching_size += 1
print("Size of maximum matching is:", matching_size)
fig, ax = plt.subplots(figsize=(7, 3))
ig.plot(
    g,
    target=ax,
    layout=g.layout_bipartite(),
    vertex_size=0.4,
    vertex_label=range(g.vcount()),
    vertex_color="lightblue",
    edge_width=[3 if e.target == matching.match_of(e.source) else 1.0 for e in g.es],
    edge_color=["red" if e.target == matching.match_of(e.source) else "black" for e in g.es]
)
plt.show()

The first failure is:

ValueError: RGBA sequence should have length 3 or 4

which is caused by the edge_color line but if you comment it out the edge_width line also fails as that variable is expected to be an int not a list.

How can this code be fixed?

These tutorials were written for the development version of igraph, not yet released. They should work correctly if you install the development version from the develop branch. See here:

You will need to compile the package yourself, which is straightforward on Linux/macOS, but may be some trouble on Windows.

1 Like

I had no idea. That is a little confusing as it says " [python-igraph 0.9.9]" in the top left of the web page.

How can the code be made to work with python-igraph 0.9.9?

@iosonofabio @tamas Can you confirm that this tutorial page belongs to the develop version? That’s what the URL suggests. How did the 0.9.9 (and also 0.9.8!) version number get into the develop docs?

Yes, the tutorial (and in fact the entire “Tutorial gallery” section) belongs to the development version, so that’s a bug on the homepage. A tell-tale sign that something is wrong is that the version switcher dropdown disappears from the header of the page that was originally linked to. I’ll look into this.

If you’d like to fix the example for the officially released version, you need to switch back to the Cairo plotting backend from Matplotlib as the Matplotlib backend in 0.9.9 does not support per-edge colors yet. Something like this will work (note that the vertex size changed as Cairo units are different):

print("Size of maximum matching is:", matching_size)

# No matplotlib figure creation here
# fig, ax = plt.subplots(figsize=(7, 3))

ig.plot(
    g,
    layout=g.layout_bipartite(),
    vertex_size=20,
    vertex_label=range(g.vcount()),
    vertex_color="lightblue",
    edge_width=[3 if e.target == matching.match_of(e.source) else 1.0 for e in g.es],
    edge_color=["red" if e.target == matching.match_of(e.source) else "black" for e in g.es]
)
1 Like

Thank you so much! This is very helpful.