Skip to content

Instantly share code, notes, and snippets.

@nickynicolson
Last active March 25, 2020 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickynicolson/d972c5fb9d1972692342448d48ce029c to your computer and use it in GitHub Desktop.
Save nickynicolson/d972c5fb9d1972692342448d48ce029c to your computer and use it in GitHub Desktop.
Display a networkx graph on a leaflet map

This gist displays a networkx graph on a leaflet map.

The graph is supplied as a weighted GEXF file, each node is expected to have Latitude and Longitude attributes.

The inputfile (GEXF format graph) and the basename for the outputfile are suplied as command line arguments.

Run as follows:

python plot.py sample.gexf output/spatial-graph
import networkx as nx
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import mplleaflet
import sys
def main():
inputfile=sys.argv[1]
outputfile_base=sys.argv[2]
# Load GEXF
g = nx.read_gexf(inputfile)
# Create node positions
pos={}
for i, node in enumerate(g.nodes()):
print(i, node)
if ('Latitude' in g.nodes[node].keys()
and 'Longitude' in g.nodes[node].keys()):
lat=g.nodes[node]['Latitude']
lng=g.nodes[node]['Longitude']
if not np.isnan(lat) and not np.isnan(lng):
pos[node]=[g.nodes[node]['Longitude'],g.nodes[node]['Latitude']]
weights = [g[u][v]['weight'] for u,v in g.edges]
fig, ax = plt.subplots()
nx.draw_networkx(g,pos=pos,width=weights)
path = outputfile_base + '.html'
mplleaflet.show(fig=ax.figure, path=path)
# TODO: automate screen capture to image file after suitable wait
if __name__ == '__main__':
main()
pandas
networkx
matplotlib
mplleaflet
<?xml version='1.0' encoding='utf-8'?>
<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<graph defaultedgetype="undirected" mode="static" name="">
<attributes class="edge" mode="static">
</attributes>
<attributes class="node" mode="static">
<attribute id="0" title="Label" type="string" />
<attribute id="1" title="Latitude" type="double" />
<attribute id="2" title="Longitude" type="double" />
<attribute id="3" title="Caption" type="string" />
</attributes>
<meta>
<creator>NetworkX 2.3</creator>
<lastmodified>24/03/2020</lastmodified>
</meta>
<nodes>
<node id="0" label="0">
<attvalues>
<attvalue for="0" value="K" />
<attvalue for="1" value="51.483055" />
<attvalue for="2" value="-0.295703" />
<attvalue for="3" value="Kew" />
</attvalues>
</node>
<node id="1" label="1">
<attvalues>
<attvalue for="0" value="P" />
<attvalue for="1" value="48.842178" />
<attvalue for="2" value="2.359820" />
<attvalue for="3" value="Paris" />
</attvalues>
</node>
<node id="2" label="2">
<attvalues>
<attvalue for="0" value="BR" />
<attvalue for="1" value="50.931194" />
<attvalue for="2" value="4.319976" />
<attvalue for="3" value="Meise" />
</attvalues>
</node>
</nodes>
<edges>
<edge id="3" source="0" target="1" weight="10.0">
<attvalues>
</attvalues>
</edge>
<edge id="4" source="0" target="2" weight="5.0">
<attvalues>
</attvalues>
</edge>
</edges>
</graph>
</gexf>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment