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]
ax.add_collection(mpl.collections.PatchCollection(patches))
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.
As discussed here, matplotlib doesn't support the automatic creation of legend handlers for polygons. Your code works fine when using Point or LineString geometries. The solution would be to create a custom legend handle (ref), which
could be done the following way:
from matplotlib.patches import Patch
fig, ax = plt.subplots(figsize=(5,5))
pmarks = []
for ctype, data in map_df.groupby('landcovermode'):
color = Palette[ctype]
label = names[ctype]
data.plot(ax=ax, color=color)
pmarks.append(Patch(facecolor=color, label=label))
handles, _ = ax.get_legend_handles_labels()
ax.legend(handles=[*handles,*pmarks], loc='lower right')
ax.set_axis_off()
This will be the output using some random test data:
![polygons_with_legend](https://i.stack.imgur.com/oqI92.png)
Best Answer
ax.set_xticks([0,1,2])
worked in my case.You might also want to try
suppress_ticks = False
, see https://stackoverflow.com/questions/18363987/basemap-how-to-remove-actual-lat-lon-lines-while-keeping-the-ticks-on-the-axis