pythonpython-3.xnetworkxpyvis

How can we plot a network graph, using pyvis, in a browser?


When I run the code below, I get this message but no network graph is displayed.

Warning: When cdn_resources is 'local' jupyter notebook has issues displaying graphics on chrome/safari. Use cdn_resources='in_line' or cdn_resources='remote' if you have issues viewing graphics in a notebook. example.html

import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt


data = [{'Circuit': 'html','Description':1, 'Duration':10.2, 'Source':'Westchester', 'Destination':'Davie', 'Picklist':1000, 'Postlist':50000.2}, 
       {'Circuit': 'html', 'Description':2, 'Duration':12.1, 'Source':'Westchester', 'Destination':'Davie', 'Picklist':3000, 'Postlist':40000.1},
       {'Circuit': 'html', 'Description':3, 'Duration':11.3, 'Source':'Westchester', 'Destination':'Davie', 'Picklist':7000, 'Postlist':50000.2}, 
       {'Circuit': 'html', 'Description':3, 'Duration':8.1, 'Source':'West', 'Destination':'San Bernardino', 'Picklist':3000, 'Postlist':40000.0},
       {'Circuit': '.net', 'Description':4, 'Duration':6.2, 'Source':'Queens', 'Destination':'San Bernardino', 'Picklist':5000, 'Postlist':6000.1}, 
       {'Circuit': '.net', 'Description':3, 'Duration':20.1, 'Source':'Queens', 'Destination':'Los Angeles', 'Picklist':5000, 'Postlist':4000.1},
       {'Circuit': '.net', 'Description':2, 'Duration':15.5, 'Source':'Brooklyn', 'Destination':'San Francisco', 'Picklist':5000, 'Postlist':9000.3},
       {'Circuit': '.net', 'Description':4, 'Duration':7.7, 'Source':'Brooklyn', 'Destination':'Davie', 'Picklist':6000, 'Postlist':10000},
       {'Circuit': '.net', 'Description':4, 'Duration':7.7, 'Source':'Los Angeles', 'Destination':'Westchester', 'Picklist':6000, 'Postlist':10000},
       {'Circuit': '.net', 'Description':4, 'Duration':7.7, 'Source':'San Berdarnino', 'Destination':'Westchester', 'Picklist':6000, 'Postlist':10000}]
df = pd.DataFrame(data)
df

import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

G = nx.from_pandas_edgelist(df, source='Source', target='Destination', edge_attr='Picklist')

from pyvis.network import Network
net = Network(notebook=True)
net.from_nx(G)
net.show('example.html')

If I add this code below, I get a static graph, but I really want the dynamic graph using the code above.

G = nx.from_pandas_edgelist(pandas_df, source = "StorageLocation", target = "StorageType", edge_attr = "Value", create_using = nx.Graph())
plt.figure(figsize=(15,12))

nx.draw(G, with_labels=True, node_color='skyblue', edge_cmap=plt.cm.Blues)
plt.show()

Solution

  • Definitely helpful! Thanks for getting me pointed in the right direction. This is what ultimately worked for me.

    import pandas as pd
    import numpy as np
    import networkx as nx
    from IPython.display import display, HTML
    
    G = nx.Graph()
    rels = [["Fred", "George"],
        ["Harry", "Rita"],
        ["Fred", "Ginny"],
        ["Tom", "Ginny"],
        ["Harry", "Ginny"],
        ["Harry", "George"],
        ["Frank", "Ginny"],
        ["Marge", "Rita"],
        ["Fred", "Rita"]]
    
    G.add_edges_from(rels)
    
    from pyvis.network import Network
    net = Network(notebook=True)
    net.from_nx(G)
    #net = Network()
    
    net.from_nx(G)
    
    net.save_graph("networkx-pyvis.html")
    HTML(filename="networkx-pyvis.html")
    

    The problem was, although I could see the graph in a Jupyter notebook, I could not see the graph in a Databricks notebook. Now the code works in a Databricks notebook, in Firefox/Mozilla. Thanks again!!