Search code examples
pythonrastershapefile

How to plot a vector map on multiple time series image


I have monthly average LMK xarray dataset (total of 12 image, and one image for each month) which was plotted with below code. I would like to plot shapefile.shp and overlay on each of these images.

fig, ax = plt.subplots(figsize=(5, 5))

lmk_plot = monthly_df.lmk.plot(figsize = (15, 30), col = "month", col_wrap = 3, cmap=cmap,
                     norm=norm, robust=True, cbar_kwargs={
        "orientation": "horizontal",
        "label": "custom label",
        "shrink": 0.5,
        "aspect": 20,
        "pad": 0.03,
        "label": "Mean LMK [%]",
    })
lmk_plot.set_titles(size = 18)
scalebar = ScaleBar(25, location = 'lower right', length_fraction = 0.25) # 1 pixel = 0.2 meter
plt.gca().add_artist(scalebar)
plt.show()

Any help much appreciated.


Solution

  • Following code did the job:

    import geopandas as gpd
    
    sf_lmk = gpd.read_file('shapefile.shp')
    lmk_plot = monthly_lmk.plot(figsize = (15, 30), col = "month", col_wrap = 3, cmap=cmap,
                         norm=norm, robust=True, cbar_kwargs={
            "orientation": "horizontal",
            "label": "custom label",
            "shrink": 0.5,
            "aspect": 20,
            "pad": 0.03,
            "label": "Mean LMK [%]",
        })
    for i, ax in enumerate(lmk_plot.axes.flat):
        sf_lmk.plot(ax=ax, color=None, alpha = 1, edgecolor='black', linewidth = 2)
    lmk_plot.set_titles(size = 18)
    scalebar = ScaleBar(25, location = 'lower right', length_fraction = 0.25) # 1 pixel = 0.2 meter
    plt.gca().add_artist(scalebar)
    plt.show()