summaryrefslogtreecommitdiff
path: root/_downloads/c79825a60948ea589076f8f2b52b4981
diff options
context:
space:
mode:
authorjarrodmillman <jarrod.millman@gmail.com>2022-12-14 17:21:13 +0000
committerjarrodmillman <jarrod.millman@gmail.com>2022-12-14 17:21:13 +0000
commit832c558e3507e5cb667a622b5372f91384ab026f (patch)
tree74481f14ab9cfb5c6984ab59b378cdc857a8180d /_downloads/c79825a60948ea589076f8f2b52b4981
parent71985f91c82bf85657dce6a74669e93ec8d29e11 (diff)
downloadnetworkx-832c558e3507e5cb667a622b5372f91384ab026f.tar.gz
Deploying to gh-pages from @ networkx/networkx@6be702047b1bb596a8010cf80911bb6ea939b1d1 🚀
Diffstat (limited to '_downloads/c79825a60948ea589076f8f2b52b4981')
-rw-r--r--_downloads/c79825a60948ea589076f8f2b52b4981/plot_points.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/_downloads/c79825a60948ea589076f8f2b52b4981/plot_points.py b/_downloads/c79825a60948ea589076f8f2b52b4981/plot_points.py
new file mode 100644
index 00000000..7d9d99df
--- /dev/null
+++ b/_downloads/c79825a60948ea589076f8f2b52b4981/plot_points.py
@@ -0,0 +1,59 @@
+"""
+=============================
+Graphs from geographic points
+=============================
+
+This example shows how to build a graph from a set of points
+using PySAL and geopandas. In this example, we'll use the famous
+set of cholera cases at the Broad Street Pump, recorded by John Snow in 1853.
+The methods shown here can also work directly with polygonal data using their
+centroids as representative points.
+"""
+
+from libpysal import weights, examples
+from contextily import add_basemap
+import matplotlib.pyplot as plt
+import networkx as nx
+import numpy as np
+import geopandas
+
+# read in example data from a geopackage file. Geopackages
+# are a format for storing geographic data that is backed
+# by sqlite. geopandas reads data relying on the fiona package,
+# providing a high-level pandas-style interface to geographic data.
+cases = geopandas.read_file("cholera_cases.gpkg")
+
+# construct the array of coordinates for the centroid
+coordinates = np.column_stack((cases.geometry.x, cases.geometry.y))
+
+# construct two different kinds of graphs:
+
+## 3-nearest neighbor graph, meaning that points are connected
+## to the three closest other points. This means every point
+## will have exactly three neighbors.
+knn3 = weights.KNN.from_dataframe(cases, k=3)
+
+## The 50-meter distance band graph will connect all pairs of points
+## that are within 50 meters from one another. This means that points
+## may have different numbers of neighbors.
+dist = weights.DistanceBand.from_array(coordinates, threshold=50)
+
+# Then, we can convert the graph to networkx object using the
+# .to_networkx() method.
+knn_graph = knn3.to_networkx()
+dist_graph = dist.to_networkx()
+
+# To plot with networkx, we need to merge the nodes back to
+# their positions in order to plot in networkx
+positions = dict(zip(knn_graph.nodes, coordinates))
+
+# plot with a nice basemap
+f, ax = plt.subplots(1, 2, figsize=(8, 4))
+for i, facet in enumerate(ax):
+ cases.plot(marker=".", color="orangered", ax=facet)
+ add_basemap(facet)
+ facet.set_title(("KNN-3", "50-meter Distance Band")[i])
+ facet.axis("off")
+nx.draw(knn_graph, positions, ax=ax[0], node_size=5, node_color="b")
+nx.draw(dist_graph, positions, ax=ax[1], node_size=5, node_color="b")
+plt.show()