Skip to content

Geo Plot

Binder

Geo Plot

In this notebook, we will see how to create geographic graph visualizations using the mid-level and high-level APIs.

%config InlineBackend.figure_format = 'retina'
%load_ext autoreload
%autoreload 2
import pickle
import matplotlib.pyplot as plt
import networkx as nx

import nxviz as nv
from nxviz import edges, layouts, nodes, plots, utils, annotate
from pyprojroot import here
/home/runner/work/nxviz/nxviz/nxviz/__init__.py:33: UserWarning: 
nxviz has a new API! Version 0.7.4 onwards, the old class-based API is being
deprecated in favour of a new API focused on advancing a grammar of network
graphics. If your plotting code depends on the old API, please consider
pinning nxviz at version 0.7.4, as the new API will break your old code.

To check out the new API, please head over to the docs at
https://ericmjl.github.io/nxviz/ to learn more. We hope you enjoy using it!

(This deprecation message will go away in version 1.0.)

  warnings.warn(

with open(here() / "docs/examples/divvy.pkl", 'rb') as f:
    G = pickle.load(f)

G_new = G.copy()
for n1, n2, d in G.edges(data=True):
    if d["count"] < 150:
        G_new.remove_edge(n1, n2)

Mid-level API

nt = utils.node_table(G_new)
pos = nodes.draw(
    G_new,
    layout_func=layouts.geo,
    group_by=None,
    sort_by=None,
    color_by="dpcapacity",
    encodings_kwargs={"size_scale": 0.0015},
)
edges.line(G_new, pos)
annotate.node_colormapping(G_new, color_by="dpcapacity")
plots.rescale(G)
plots.aspect_equal()
plots.despine()
/home/runner/work/nxviz/nxviz/.pixi/envs/docs/lib/python3.13/site-packages/matplotlib/patches.py:1648: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`
  center = (self.convert_xunits(self._center[0]),
/home/runner/work/nxviz/nxviz/.pixi/envs/docs/lib/python3.13/site-packages/matplotlib/patches.py:1649: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`
  self.convert_yunits(self._center[1]))

No description has been provided for this image

High-level API

from nxviz import annotate
nv.geo(G_new, node_color_by="dpcapacity")
annotate.node_colormapping(G_new, color_by="dpcapacity")
/home/runner/work/nxviz/nxviz/.pixi/envs/docs/lib/python3.13/site-packages/matplotlib/patches.py:1648: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`
  center = (self.convert_xunits(self._center[0]),
/home/runner/work/nxviz/nxviz/.pixi/envs/docs/lib/python3.13/site-packages/matplotlib/patches.py:1649: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`
  self.convert_yunits(self._center[1]))

No description has been provided for this image