Descartes
Descartes is a Python library that enables the use of geometric objects, typically from the Shapely library, as Matplotlib paths and patches. It provides a convenient way to integrate vector geometries into Matplotlib plots. The current version is 1.1.0, released on January 17, 2017. Due to its last release date, the project has a very slow or effectively ceased release cadence, leading to compatibility challenges with newer dependencies.
Warnings
- breaking Descartes 1.1.0 is incompatible with Shapely versions 2.0 and newer. Using `PolygonPatch` (or other patches) with `Shapely >= 2.0` will result in an `IndexError: too many indices for array` due to changes in how Shapely exposes exterior/interior coordinates.
- deprecated The Descartes library is no longer actively maintained; its last release was in January 2017. This means it will not receive updates for new features, bug fixes, or official compatibility with newer versions of Python or its dependencies (like Matplotlib or Shapely). Other libraries that previously relied on Descartes are migrating away from it.
- gotcha While Descartes handles basic geometric objects, for more complex GIS plotting needs (e.g., coordinate system transformations, basemaps), you will likely need to integrate it with other libraries like Matplotlib's Basemap or GeoPandas (though GeoPandas is moving away from Descartes).
Install
-
pip install descartes
Imports
- PolygonPatch
from descartes import PolygonPatch
- PointPatch
from descartes import PointPatch
- LineStringPatch
from descartes import LineStringPatch
Quickstart
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
from descartes import PolygonPatch
# Define a simple polygon using Shapely
polygon = Polygon([(0, 0), (1, 1), (0, 1), (0, 0)])
# Create a Matplotlib figure and axes
fig, ax = plt.subplots()
# Create a PolygonPatch from the Shapely Polygon
# You can customize color (fc), edge color (ec), alpha, etc.
patch = PolygonPatch(polygon, fc='blue', ec='black', alpha=0.5, zorder=2)
# Add the patch to the axes
ax.add_patch(patch)
# Set axes limits to display the polygon
minx, miny, maxx, maxy = polygon.bounds
ax.set_xlim(minx - 0.1, maxx + 0.1)
ax.set_ylim(miny - 0.1, maxy + 0.1)
ax.set_aspect('equal', adjustable='box') # Keep aspect ratio
plt.title("Shapely Polygon plotted with Descartes")
plt.show()