python-igraph icon indicating copy to clipboard operation
python-igraph copied to clipboard

Feature Request: Edge Style (Solid and Dashed Lines) in python-igraph

Open WANGchuang715 opened this issue 9 months ago • 8 comments

WANGchuang715 avatar May 20 '25 06:05 WANGchuang715

Please elaborate? I believe this is already working

iosonofabio avatar May 20 '25 06:05 iosonofabio

Thank you for your reply. I have checked all the available parameters for edges, but I didn't find any parameter similar to edge.lty in R to control the drawing of solid and dashed lines. Is there any parameter that can achieve this? I am using the Matplotlib backend.

Image

WANGchuang715 avatar May 20 '25 10:05 WANGchuang715

Try edge_linestyle

iosonofabio avatar May 20 '25 10:05 iosonofabio

Thank you for your reply. I tested the edge linestyle parameter but it doesn't seem to work as expected. It only draws solid lines. Is there something wrong with my code or is this a known issue?

Here is my code:

import igraph as ig
import matplotlib.pyplot as plt

g = ig.Graph(edges=[[0, 1], [1, 2], [2, 3], [3, 0]])
edge_linestyles = ['-', '--', ':', '-.']
g.es['linestyle'] = edge_linestyles
fig, ax = plt.subplots()
ig.plot(g, target=ax, edge_linestyle=g.es['linestyle'])
plt.savefig("graph_with_different_linestyles.png", dpi=300)

Image

WANGchuang715 avatar May 20 '25 10:05 WANGchuang715

Not sure ATM, I'm traveling with the kids so it might take a day or two before I can try this out.

Let's see if anyone else happens to know, otherwise I'll get back to you

iosonofabio avatar May 20 '25 10:05 iosonofabio

I am developing a new package to improve plotting of graphs/networks in Python using igraph or networkx, it's called iplotx. It's designed to make all these things easy. In your case:

import igraph as ig
import matplotlib.pyplot as plt
import iplotx as ipx

g = ig.Graph(edges=[[0, 1], [1, 2], [2, 3], [3, 0]])
layout = [[0, -1], [1, 0], [0, 1], [-1, 0]]
style = {
    'vertex': {
        'facecolor': 'red',
        'edgecolor': 'black',
        'linewidth': 2,
    },
    'edge': {
        'linestyle': ['-', '--', ':', '-.'],
    }
}
fig, ax = plt.subplots()
ipx.plot(
    network=g,
    layout=layout,
    style=style,
    ax=ax,
)
plt.ion()
plt.show()

and you'll end up with what you want:

Image

NOTE: iplotx does NOT compute layouts (for now), so you can use igraph for that.

iosonofabio avatar May 25 '25 13:05 iosonofabio

Link to the package: https://github.com/fabilab/iplotx You can install it from pip

iosonofabio avatar May 27 '25 11:05 iosonofabio

Link to the package: https://github.com/fabilab/iplotx You can install it from pip

Thank you for your reply

WANGchuang715 avatar May 29 '25 09:05 WANGchuang715