Skip to content

Instantly share code, notes, and snippets.

@fralc
Last active April 1, 2025 13:20
Show Gist options
  • Select an option

  • Save fralc/73a64f8384dcc7c6412a113286ed44d9 to your computer and use it in GitHub Desktop.

Select an option

Save fralc/73a64f8384dcc7c6412a113286ed44d9 to your computer and use it in GitHub Desktop.
Geopandas `gdf.sample_points()` x sorting proof
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from shapely.geometry import Polygon
polygon = Polygon(((0, 0), (0, 1), (5, 1), (5, 0)))
polygon_gdf = gpd.GeoDataFrame({'geometry': [polygon]}, geometry='geometry')
points = polygon_gdf.sample_points(size=1000)
# ax = polygon_gdf.boundary.plot()
# points.plot(ax=ax, alpha=.5)
points = points.explode().reset_index()
# points = points.explode().sample(frac=1).reset_index() # sampling tackle the x-along issue
points['sorter'] = np.arange(0, points.shape[0])
ax1 = points.plot(column='sorter', cmap='gist_ncar')
# ax2 = points.geometry.x.plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment