Visualise the edges based on their weight

Hi all,

I’m very new with graph network and just started with DGL. I was wondering whether we can visualise edges differently (e.g. by colour, thickness, distance,etc) based on each of their weight. It helps me understand how this package works in my problem.

Thanks a lot

Best

You may want to take a look at this thread first.

THanks @mufeili,

I tried with the code, but it threw me an error, saying AssertionError: Expected g to be an networkx.DiGraphobject, got <class 'dgl.graph.DGLGraph'>

is it because I converted my graph to dgl with this lines
G=nx.Graph()
dgl_G=dgl.DGLGraph(G)
dgl_G.from_networkx(G, edge_attrs=[‘weight’])

But it seems that your function takes DiGraph instead of Graph. Thanks so much.

best

Yes, the code is based on NetworkX and you should convert DGLGraph to networkx.DiGraph first.

Thank @mufeili,

I converted my graph to networkx.DiGraph, but now it throws a new error " RuntimeError: bool value of Tensor with more than one value is ambiguous"

I printed my converted edges below with g.edges():
OutEdgeView([(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7)])

Do you know what could be the cause?

Where was the error thrown, with g.to_networkx() or the visualization part?

Hey @mufeili,

It was in visualization part when I execute

plot(g, sorted(g.edges()), ax=ax, nodes_pos=nx.random_layout(g))

But I think it generated that error because I have node(s) without any edge connecting to it. So, when I remove those nodes, it now throws this error from matloptlib

ValueError: RGBA sequence should have length 3 or 4

Now, I’m not sure that about this error yet. Do you have an idea about this? Thanks so much

It seems that there are some problems with your attention argument. Could you please share a code snippet for reproducing the issue?

I’m sorry for the late response @mufeili, but here is the code i used

def plot(g, attention, ax, nodes_to_plot=None, nodes_labels=None,
         edges_to_plot=None, nodes_pos=None, nodes_colors=None,
         edge_colormap=plt.cm.Reds):
   
    if nodes_to_plot is None:
        nodes_to_plot = sorted(g.nodes())
    if edges_to_plot is None:
        assert isinstance(g, nx.DiGraph), 'Expected g to be an networkx.DiGraph' \
                                          'object, got {}.'.format(type(g))
        edges_to_plot = sorted(g.edges())
    nx.draw_networkx_edges(g, nodes_pos, edgelist=edges_to_plot,
                           edge_color=attention, edge_cmap=edge_colormap,
                           width=2, alpha=0.5, ax=ax, edge_vmin=0,
                           edge_vmax=1)

    if nodes_colors is None:
        nodes_colors = sns.color_palette("deep", max(nodes_labels) + 1)

    nx.draw_networkx_nodes(g, nodes_pos, nodelist=nodes_to_plot, ax=ax, node_size=30,
                           node_color=[nodes_colors[nodes_labels[v - 1]] for v in nodes_to_plot],
                           with_labels=False, alpha=0.9)
    

print(type(g_dgl))
dg = g_dgl.to_networkx().to_directed()
print(type(dg))
dg.edges()

#Y.reshape(Y.shape[0])
grEd = dg.edges()
grEd = grEd.reshape(grEd.shape[1])

fig, ax = plt.subplots()
plot(dg, sorted(grEd), ax=ax, nodes_pos=nx.random_layout(dg))
ax.set_axis_off()
sm = plt.cm.ScalarMappable(cmap=plt.cm.Reds, norm=plt.Normalize(vmin=0, vmax=1))
sm.set_array([])
plt.colorbar(sm, fraction=0.046, pad=0.01)
plt.show()

I’ve modified your code a bit and see the code below for an example.

import matplotlib.pyplot as plt
import networkx as nx
import seaborn as sns
import torch
from dgl import DGLGraph

def plot(g, attention, ax, nodes_to_plot=None, nodes_labels=None,
         edges_to_plot=None, nodes_pos=None, nodes_colors=None,
         edge_colormap=plt.cm.Reds):
   
    if nodes_to_plot is None:
        nodes_to_plot = sorted(g.nodes())
    if edges_to_plot is None:
        assert isinstance(g, nx.DiGraph), 'Expected g to be an networkx.DiGraph' \
                                          'object, got {}.'.format(type(g))
        edges_to_plot = sorted(g.edges())
    nx.draw_networkx_edges(g, nodes_pos, edgelist=edges_to_plot,
                           edge_color=attention, edge_cmap=edge_colormap,
                           width=2, alpha=0.5, ax=ax, edge_vmin=0,
                           edge_vmax=1)

    if nodes_colors is None:
        nodes_colors = sns.color_palette("deep", max(nodes_labels) + 1)

    nx.draw_networkx_nodes(g, nodes_pos, nodelist=nodes_to_plot, ax=ax, node_size=30,
                           node_color=[nodes_colors[nodes_labels[v - 1]] for v in nodes_to_plot],
                           with_labels=False, alpha=0.9)
    
g_dgl = DGLGraph([(0, 1), (1, 2), (1, 3)])
print(type(g_dgl))
dg = g_dgl.to_networkx().to_directed()
print(type(dg))
dg.edges()

#Y.reshape(Y.shape[0])
grEd = dg.edges()
print(grEd)

fig, ax = plt.subplots()
plot(dg, [0.2, 0.6, 0.6], ax=ax, nodes_pos=nx.random_layout(dg), nodes_labels=[0, 1, 2])
ax.set_axis_off()
sm = plt.cm.ScalarMappable(cmap=plt.cm.Reds, norm=plt.Normalize(vmin=0, vmax=1))
sm.set_array([])
plt.colorbar(sm, fraction=0.046, pad=0.01)
plt.show()

Thanks @mufeili. it does work.