|
| 1 | +""" |
| 2 | +.. _tutorials-iplotx: |
| 3 | +
|
| 4 | +============================== |
| 5 | +Visualising graphs with iplotx |
| 6 | +============================== |
| 7 | +``iplotx`` (https://iplotx.readthedocs.io) is a library for visualisation of graphs/networks |
| 8 | +with direct compatibility with both igraph and NetworkX. It uses ``matplotlib`` behind the |
| 9 | +scenes so the results are compatible with the current igraph matplotlib backend and many |
| 10 | +additional chart types (e.g. bar charts, annotations). |
| 11 | +
|
| 12 | +Compared to the standard visualisations shipped with igraph, ``iplotx`` offers: |
| 13 | +
|
| 14 | +- More styling options |
| 15 | +- More consistent behaviour across DPI resolutions and backends |
| 16 | +- More consistent matplotlib artists for plot editing and animation |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +import igraph as ig |
| 21 | +import iplotx as ipx |
| 22 | + |
| 23 | +# Construct a graph with 5 vertices |
| 24 | +n_vertices = 5 |
| 25 | +edges = [(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (3, 4)] |
| 26 | +g = ig.Graph(n_vertices, edges) |
| 27 | + |
| 28 | +# Set attributes for the graph, nodes, and edges |
| 29 | +g["title"] = "Small Social Network" |
| 30 | +g.vs["name"] = [ |
| 31 | + "Daniel Morillas", |
| 32 | + "Kathy Archer", |
| 33 | + "Kyle Ding", |
| 34 | + "Joshua Walton", |
| 35 | + "Jana Hoyer", |
| 36 | +] |
| 37 | +g.vs["gender"] = ["M", "F", "F", "M", "F"] |
| 38 | +g.es["married"] = [False, False, False, False, False, False, False, True] |
| 39 | + |
| 40 | +# Set individual attributes |
| 41 | +g.vs[1]["name"] = "Kathy Morillas" |
| 42 | +g.es[0]["married"] = True |
| 43 | + |
| 44 | +# Plot using iplotx |
| 45 | +ipx.network( |
| 46 | + g, |
| 47 | + layout="circle", # print nodes in a circular layout |
| 48 | + vertex_marker="s", |
| 49 | + vertex_size=45, |
| 50 | + vertex_linewidth=2, |
| 51 | + vertex_facecolor=[ |
| 52 | + "lightblue" if gender == "M" else "deeppink" for gender in g.vs["gender"] |
| 53 | + ], |
| 54 | + vertex_label_color=[ |
| 55 | + "black" if gender == "M" else "white" for gender in g.vs["gender"] |
| 56 | + ], |
| 57 | + vertex_edgecolor="black", |
| 58 | + vertex_labels=[name.replace(" ", "\n") for name in g.vs["name"]], |
| 59 | + edge_linewidth=[2 if married else 1 for married in g.es["married"]], |
| 60 | + edge_color=["#7142cf" if married else "#AAA" for married in g.es["married"]], |
| 61 | + edge_padding=3, |
| 62 | + aspect=1.0, |
| 63 | +) |
0 commit comments