Skip to content

Instantly share code, notes, and snippets.

@ajfriend
Created March 19, 2025 03:43
Show Gist options
  • Save ajfriend/93e75cd48f570ca0225fa334955870fb to your computer and use it in GitHub Desktop.
Save ajfriend/93e75cd48f570ca0225fa334955870fb to your computer and use it in GitHub Desktop.
import h3
import geopandas
import pandas as pd
import matplotlib.pyplot as plt
import contextily as cx
h = '8c196938064c5ff'
# convert to h3.LatLngPoly, which is compatible with
# geopandas via __geo_interface__
shape = h3.cells_to_h3shape([h])
df = geopandas.GeoDataFrame(
{'geometry': [shape]},
crs='EPSG:4326', # H3 provides coordinates in WGS84
)
df = df.to_crs('EPSG:3857') # convert to web mercator for plotting
fig, ax = plt.subplots(figsize=(4,4))
df.plot(
ax = ax,
alpha = 0.2,
edgecolor = 'k',
)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
buffer = .8
x1, y1, x2, y2 = df.total_bounds
dx = buffer*(x2 - x1)
dy = buffer*(x2 - x1)
ax.set_xlim(x1 - dx, x2 + dx)
ax.set_ylim(y1 - dy, y2 + dy)
cx.add_basemap(
ax,
crs = df.crs,
source = cx.providers.CartoDB.PositronNoLabels,
zoom = 20,
)
fig.tight_layout()
fig.savefig('temp.png')
@ajfriend
Copy link
Author

temp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment