How to visualize a graph from DGL's datasets?

4.5k views Asked by At

The following snippet comes from the tutorial https://cnvrg.io/graph-neural-networks/. How can I visualize a graph from the dataset? Using something like matplotlib if possible.

import dgl
import torch
import torch.nn as nn
import torch.nn.functional as F
import dgl.data
dataset = dgl.data.CoraGraphDataset()
g = dataset[0]
1

There are 1 answers

2
LemonPy On
import dgl.data
import matplotlib.pyplot as plt
import networkx as nx

dataset = dgl.data.CoraGraphDataset()
g = dataset[0]
options = {
    'node_color': 'black',
    'node_size': 20,
    'width': 1,
}
G = dgl.to_networkx(g)
plt.figure(figsize=[15,7])
nx.draw(G, **options)

It is a huge graph so you might have to play with the sizes (node, width, figure, etc.). Here are some useful links: