Errors using edge_color and edge_width in igraph.plot()

Hi everyone

I was trying to use igraph to generate a network with variant edge width, so I tried one example with tutorial example. Here is the link: Quick Start

However, this tutorial is not working on my environment. I removed the edge_width and edge_color in ig.plot() then it works and plotted something, of course, without variances in edge width. I don’t know whether there is anything wrong on my python environment. I have attached the tarceback here.

Thanks so much for help!

Here is the traceback:
For edge_width:

Traceback (most recent call last):
  File "H:/my/code folder/nonvVisualisation/testing/network_igraph.py", line 130, in <module>
    __demo_quickstart()
  File "H:/my/code folder/nonvVisualisation/testing/network_igraph.py", line 110, in __demo_quickstart
    edge_width=[7 if married else 1 for married in g.es["married"]],
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\igraph\drawing\__init__.py", line 475, in plot
    result.draw(obj, *args, **kwds)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\igraph\drawing\graph.py", line 1351, in draw
    zorder=ezorder[ie],
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 4116, in __init__
    Patch.__init__(self, **kwargs)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 103, in __init__
    self.update(kwargs)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 888, in update
    for k, v in props.items()]
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 888, in <listcomp>
    for k, v in props.items()]
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 882, in _update_property
    return func(v)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 371, in set_lw
    return self.set_linewidth(lw)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 362, in set_linewidth
    self._linewidth = float(w)
TypeError: float() argument must be a string or a number, not 'list'

For edge_color:

Traceback (most recent call last):
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\colors.py", line 169, in to_rgba
    rgba = _colors_full_map.cache[c, alpha]
TypeError: unhashable type: 'list'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "H:/my/code folder/nonvVisualisation/testing/network_igraph.py", line 130, in <module>
    __demo_quickstart()
  File "H:/my/code folder/nonvVisualisation/testing/network_igraph.py", line 111, in __demo_quickstart
    edge_color=["#7142cf" if married else "#AAA" for married in g.es["married"]]
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\igraph\drawing\__init__.py", line 475, in plot
    result.draw(obj, *args, **kwds)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\igraph\drawing\graph.py", line 1351, in draw
    zorder=ezorder[ie],
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 4116, in __init__
    Patch.__init__(self, **kwargs)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 85, in __init__
    self.set_color(color)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 332, in set_color
    self.set_facecolor(c)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 315, in set_facecolor
    self._set_facecolor(color)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\patches.py", line 305, in _set_facecolor
    self._facecolor = colors.to_rgba(color, alpha)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\colors.py", line 171, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "C:\Users\zuhuh\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\colors.py", line 222, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: ['#7142cf', '#AAA', '#AAA', '#AAA', '#AAA', '#AAA', '#AAA', '#7142cf']

Process finished with exit code 1

Hello!

Can you please post the following things here?

  • igraph version number
  • matplotlib version number
  • a copy of the network_igraph.py that you are trying to run

I’m asking because I’ve just tried to run the code from the tutorial that you linked to, and it works just fine on my machine. (Python 3.9.13, igraph 0.9.10, matplotlib 3.5.1).

Hi tamas

Thanks for reply. I was using Python 3.7.3, igrapg 0.9.11, matplotlib 3.3.0. Here is the code. It is just a copy and paste from the tutorial code.

I also tried to install Python, igraph, and matplotlib with the version you mentioned, but it still cannot run.

# Construct a graph with 3 vertices
n_vertices = 3
edges = [(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (3, 4)]
g = ig.Graph(n_vertices, edges)

# Set attributes for the graph, nodes, and edges
g["title"] = "Small Social Network"
g.vs["name"] = ["Daniel Morillas", "Kathy Archer", "Kyle Ding", "Joshua Walton", "Jana Hoyer"]
g.vs["gender"] = ["M", "F", "F", "M", "F"]
g.es["married"] = [False, False, False, False, False, False, False, True]

# Set individual attributes
g.vs[1]["name"] = "Kathy Morillas"
g.es[0]["married"] = True

# Plot in matplotlib
# Note that attributes can be set globally (e.g. vertex_size), or set individually using arrays (e.g. vertex_color)
fig, ax = plt.subplots(figsize=(5,5))
ig.plot(
    g,
    target=ax,
    layout="circle", # print nodes in a circular layout
    vertex_size=0.1,
    vertex_color=["steelblue" if gender == "M" else "salmon" for gender in g.vs["gender"]],
    vertex_frame_width=4.0,
    vertex_frame_color="white",
    vertex_label=g.vs["name"],
    vertex_label_size=7.0,
    edge_width=[2 if married else 1 for married in g.es["married"]],
    # edge_color=["#7142cf" if married else "#AAA" for married in g.es["married"]]
)

plt.show()

# Save the graph as an image file
fig.savefig('social_network.png')
fig.savefig('social_network.jpg')
fig.savefig('social_network.pdf')

# Export and import a graph as a GML file.
g.save("social_network.gml")
g = ig.load("social_network.gml")

Thanks for the report; now I managed to reproduce the problem. It looks like the plot on the homepage was accidentally generated with the development branch of igraph and not the released version; the development branch has supported varying edge widths and colors for the Matplotlib backend for a while now, while the released version supports it only with the Cairo backend so far.

I have committed a patch in the master branch to support varying edge widths and colors for Matplotlib in the master branch as well:

You can make the same modifications yourself in the source code of igraph, or you can switch to the Cairo backend for plotting (basically, use a filename as target= and not a Matplotlib axes object). I’ll release a patch version soon.

Thanks tamas, the problem is solved! You guys are awesome!