In Depth 4 - Drawing multilayer-style#

Here we show the fuctionalities and parameters of xgi.draw_multilayer().

[1]:
import matplotlib.pyplot as plt

import xgi

Les us first create a small toy hypergraph containing edges of different sizes.

[2]:
H = xgi.fast_random_hypergraph(10, ps=[0.2, 0.05, 0.05], seed=2)
[3]:
ax = plt.axes(projection="3d")
xgi.draw_multilayer(H, ax=ax)

plt.show()
../../_images/api_tutorials_in_depth_4_5_0.png

Basics#

Notice that this function returns a tuple (ax, collections) where collections is a tuple (node_collection, edge_collection). The collections can be used to plot colorbars as we will see later.

The color, linewidth, transparancy, and style of the hyperedges can all be customised, for example with single values:

[4]:
pos = xgi.barycenter_spring_layout(H, seed=1)

ax = plt.axes(projection="3d")
xgi.draw_multilayer(
    H,
    ax=ax,
    pos=pos,
    node_fc="yellow",
    node_shape="s",
    dyad_color="r",
    dyad_style="--",
    dyad_lw=2,
    edge_fc="b",
    layer_color="g",
)

plt.show()
../../_images/api_tutorials_in_depth_4_9_0.png

Or with multiple values:

[5]:
pos = xgi.barycenter_spring_layout(H, seed=1)

ax = plt.axes(projection="3d")
xgi.draw_multilayer(
    H,
    ax=ax,
    pos=pos,
    node_fc=["red"] + ["white"] * 9,
    dyad_color=["violet"] + ["k"] * 9,
    edge_fc="b",
    layer_color=["b", "g", "y"],
)

plt.show()
../../_images/api_tutorials_in_depth_4_11_0.png

Arrays of floats and colormaps#

In XGI, you can easily color hyperedges according to an EdgeStat, or just an array or a dict with float values:

[6]:
pos = xgi.barycenter_spring_layout(H, seed=1)

ax = plt.axes(projection="3d")
ax, collections = xgi.draw_multilayer(
    H, ax=ax, pos=pos, edge_fc=H.edges.size, node_fc=H.nodes.degree
)

node_coll, edge_coll = collections

plt.colorbar(node_coll, label="nodes")
plt.colorbar(edge_coll, label="edges")
[6]:
<matplotlib.colorbar.Colorbar at 0x12da50a10>
../../_images/api_tutorials_in_depth_4_14_1.png

By default, the colormaps used are “crest_r” and “Reds”. These can be changed:

[7]:
pos = xgi.barycenter_spring_layout(H, seed=1)

ax = plt.axes(projection="3d")
ax, collections = xgi.draw_multilayer(
    H,
    ax=ax,
    pos=pos,
    edge_fc=H.edges.size,
    node_fc=H.nodes.degree,
    node_fc_cmap="Blues",
    edge_fc_cmap="viridis",
    alpha=0.3,
)

node_coll, edge_coll = collections

plt.colorbar(node_coll, label="nodes")
plt.colorbar(edge_coll, label="edges")
[7]:
<matplotlib.colorbar.Colorbar at 0x12dba1bb0>
../../_images/api_tutorials_in_depth_4_16_1.png

You can even have a cmap for the layers instead:

[8]:
pos = xgi.barycenter_spring_layout(H, seed=1)

ax = plt.axes(projection="3d")
ax, collections = xgi.draw_multilayer(
    H, ax=ax, pos=pos, edge_fc="grey", layer_color=[2, 3, 4], layer_cmap="viridis"
)

node_coll, edge_coll = collections

plt.show()
../../_images/api_tutorials_in_depth_4_18_0.png

Set the height between layers and the view angles#

By default the height between layers is sep=0.4:

[11]:
ax = plt.axes(projection="3d")
xgi.draw_multilayer(H, ax=ax, sep=0.4)

plt.show()
../../_images/api_tutorials_in_depth_4_26_0.png

This can be changed!

[12]:
ax = plt.axes(projection="3d")
xgi.draw_multilayer(H, ax=ax, sep=0.1)

plt.show()

ax = plt.axes(projection="3d")
xgi.draw_multilayer(H, ax=ax, sep=1)

plt.show()
../../_images/api_tutorials_in_depth_4_28_0.png
../../_images/api_tutorials_in_depth_4_28_1.png

Similarly, the default view angles are h_angle=10, v_angle=20, but can be changed:

[13]:
ax = plt.axes(projection="3d")
xgi.draw_multilayer(H, ax=ax, h_angle=5, v_angle=50)

plt.show()
../../_images/api_tutorials_in_depth_4_30_0.png
[14]:
ax = plt.axes(projection="3d")
xgi.draw_multilayer(H, ax=ax, h_angle=70, v_angle=10)

plt.show()
../../_images/api_tutorials_in_depth_4_31_0.png

Control axis size#

You can control the size of the axis by passing a custom made Axis3d like so:

[15]:
_, ax = plt.subplots(figsize=(4, 4), subplot_kw={"projection": "3d"})
xgi.draw_multilayer(H, ax=ax)

plt.show()
../../_images/api_tutorials_in_depth_4_34_0.png
[16]:
plt.close(
    "all"
)  # closes existing ax3d to avoid bugs in next notebooks when running notebook tests