File: simplify.py

package info (click to toggle)
python-igraph 1.0.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: ansic: 25,204; python: 22,041; sh: 118; makefile: 35; sed: 2
file content (90 lines) | stat: -rw-r--r-- 1,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
========
Simplify
========

This example shows how to remove self loops and multiple edges using :meth:`igraph.GraphBase.simplify`.
"""

import igraph as ig
import matplotlib.pyplot as plt

# %%
# We start with a graph that includes loops and multiedges:
g1 = ig.Graph(
    [
        (0, 1),
        (1, 2),
        (2, 3),
        (3, 4),
        (4, 0),
        (0, 0),
        (1, 4),
        (1, 4),
        (0, 2),
        (2, 4),
        (2, 4),
        (2, 4),
        (3, 3),
    ],
)

# %%
# To simplify the graph, we must remember that the function operates in place,
# i.e. directly changes the graph that it is run on. So we need to first make a
# copy of our graph, and then simplify that copy to keep the original graph
# untouched:
g2 = g1.copy()
g2.simplify()

# %%
# We can then proceed to plot both graphs to see the difference. First, let's
# choose a consistent visual style:
visual_style = {
    "vertex_color": "lightblue",
    "vertex_size": 20,
    "vertex_label": [0, 1, 2, 3, 4],
}

# %%
# And finally, let's plot them in twin axes, with rectangular frames around
# each plot:
fig, axs = plt.subplots(1, 2, sharex=True, sharey=True)
ig.plot(
    g1,
    layout="circle",
    target=axs[0],
    **visual_style,
)
ig.plot(
    g2,
    layout="circle",
    target=axs[1],
    **visual_style,
)
axs[0].set_title("Multigraph...")
axs[1].set_title("...simplified")
# Draw rectangles around axes
axs[0].add_patch(
    plt.Rectangle(
        (0, 0),
        1,
        1,
        fc="none",
        ec="k",
        lw=4,
        transform=axs[0].transAxes,
    )
)
axs[1].add_patch(
    plt.Rectangle(
        (0, 0),
        1,
        1,
        fc="none",
        ec="k",
        lw=4,
        transform=axs[1].transAxes,
    )
)
plt.show()