Home > Software design >  Plotting multiple graphs with matplotlib subplots does not show edge colors
Plotting multiple graphs with matplotlib subplots does not show edge colors

Time:07-21

I am using igraph to analyze a network and find a specific kind of triad as subgraphs of the main network. I successfully did that but now I'm trying to plot these subgraphs in a multipanel figure using matplotlib.

import pandas as pd
import igraph as ig
import matplotlib.pyplot as plt
import math

# some long hidden code here to generate the main network (g)

# the triad pattern
ffl = g.Isoclass(n=3, cls=7, directed=True)

# finding the subgraphs
sub = g.get_subisomorphisms_vf2(ffl)

# trying to plot the subgraphs
ffls = []
for i in range(len(sub)):
    vtx = g.vs.select(sub[i])
    ffl = g.subgraph(vtx)
    ffl.vs['name'] = g.vs[sub[i]]['name']
    ffls.append(ffl)

visual_style = {'layout': 'reingold_tilford',
                'vertex_label_size': 15}

fig, axs = plt.subplots(nrows=7, ncols=4, figsize=(15,25))
plt.subplots_adjust(wspace=0.5, hspace=0.5)
axs = axs.ravel()
for ax in axs:
    ax.set_axis_off()
for ffl, ax in zip(ffls, axs):
    ig.plot(ffl, target=ax, vertex_label=ffl.vs['name'], **visual_style)
plt.show()

The resulting figure shows all the subgraphs as intended but without the colors of the edges (that I already defined in the main network). I know the colors are being captured in the subgraphs because when I plot them individually the colors are there (also the attributes are correctly assigned to the edges). Only when I try to plot them all together that I have this "problem".

What I am doing wrong? It is possible to show the edge colors when using plt.subplots()? Can someone help me?

CodePudding user response:

I've tried setting edge_color, color, face_color in the plot function but none of them worked. Actually only edge_color works but it sets the color for all edges.

The only way that worked was setting the edgecolor in the axes' children directly after plot. Take a look at the solution below:

for ffl, ax in zip(ffls, axs):
    ig.plot(ffl, target=ax, vertex_label=ffl.vs['name'], **visual_style)
    ax.set_xlim(-1, 1.25)
    ax.set_ylim(-0.5, 1.25)
    children = ax.get_children()
    for i, e in enumerate(ffl.es):
        edge = children[i   4] # Skip the vertex and the 3 labels
        edge.set_edgecolor(e['color'])
        edge.set_in_layout(True)

This is the resulting screenshot:

Screenshot

  • Related