I need to plot a shapefile on top of a raster. I would like to use the python package rasterio and some combination of fiona or geopandas to load the shapefile.

I found an example in the rasterio documentation but it doesn't provide code for the actual plotting.

enter image description here

I have tried loading the raster with rasterio, loading the shapefile using fiona, calling imshow on the raster, and calling plot on the shapefile. However, the shapefile does not appear on the plot.

1 Answer 1


I added that recipe to the rasterio documentation. Since it was such a simple shape, in this case I just unzipped the coords in the single record contained by the shapefile. That is, x, y = zip(*features[0]['coordinates'][0]), and then just plot.

More generally, I use PolygonPatch from descartes, and matplotlib.collections.

import fiona
import rasterio
import rasterio.plot
import matplotlib as mpl
from descartes import PolygonPatch

src = rasterio.open("tests/data/RGB.byte.tif")

with fiona.open("tests/data/box.shp", "r") as shapefile:
    features = [feature["geometry"] for feature in shapefile]

rasterio.plot.show((src, 1))
ax = mpl.pyplot.gca()

patches = [PolygonPatch(feature) for feature in features]

The appearance of the shapes can be customized with keywords like edgecolor or facecolor passed to PolygonPatch. To produce a thick red line as in the example, replace the last two lines in the example above with:

patches = [PolygonPatch(feature, edgecolor="red", facecolor="none", linewidth=2) for feature in features]
ax.add_collection(mpl.collections.PatchCollection(patches, match_original=True))

The match_original keyword is necessary in the second example because parameters like facecolor and edgecolor can also be set in PatchCollection, and the default is to ignore settings of the passed in patches in favor of those set by PatchCollection. Doing so using PolygonPatch gives more flexibility to set different colors, widths, etc., for each patch that you add.

  • I am having trouble importing rasterio.plot. Any ideas? I am using rasterio version 0.25.
    – jsta
    Commented May 20, 2016 at 13:31
  • 1
    rasterio.plot was only recently introduced. Use rasterio.tool instead for older versions.
    – jdmcbr
    Commented May 20, 2016 at 23:29
  • 1
    is there a prerequisite that tiff and shp file should be in the same projection?
    – gis_wild
    Commented Oct 13, 2018 at 8:50
  • 1
    Yes, the bounds of the raster in whatever projection it is in are used to set the extent of the plot window, which then allows the vector layer to be properly overlaid.
    – jdmcbr
    Commented Oct 16, 2018 at 3:45
  • @jdmcbr, thanks for adding the recipe here. It would be great to add it to the original rasterio documentation, I ran into the same problem. :)
    – edge-case
    Commented Jul 25, 2019 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.