0

I am interpolating point data to generate dynamic flood inundation maps in a for loop. This produces a flood map in each iteration where the pixel values show the probability of water presence. However, I'm unable to make the dry pixels (values < 0.2) transparent so the base map can be seen when added.

Initially I created a geodataframe of predicted probabilities. Then generated raster files using rasterio library using the condition ( <0.2 = np.nan) to set dry pixels. My code is:

    geom = [Point(xy) for xy in zip(df.X, df.Y)]
    crs = {'init': 'epsg:27700'}
    gdf = geopandas.GeoDataFrame(df, crs=crs, geometry=geom)

    ###Rasterization

    #Set up filenames

    rst_fn = 'dem_9m_study_area.asc' #template raster

    out_fn = 'Raster.tif'

    rst = rasterio.open(rst_fn)

    #copy and update the metadata from the input raster for the output

    meta = rst.meta.copy()
    meta.update(compress='lzw')

    with rasterio.open(out_fn, 'w', **meta) as out:
        out_arr = out.read(1)
        shapes = ((geom,value) for geom, value in zip(gdf.geometry, gdf.Prob))
        burned = features.rasterize(shapes=shapes, fill=0, out=out_arr, transform=out.transform)
        burned[burned < 0.2] = np.nan
        out.write_band(1, burned)

Now i would like to import the saved raster and plot it over a background raster which is also in the same coordinate (EPSG:27700), and also show the color bar.

I tried this:

    plt.figure(num=None, figsize=(10, 8), dpi=80, facecolor='w', edgecolor='k')
    plt.imshow(burned, cmap='Blues')
    plt.colorbar()
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.title('Flood Extent: T=%i h'%op)

Extent with values <0.2 set to nan

this works fine without the background though the x and y coordinates are not showing correctly. But does not work if I add this to the above code:

bmap = rasterio.open("background_upton.tif") #import basemap
    src = bmap.read(1)
    plt.imshow(src, cmap = 'pink')

I also tried the method described in "Adding a background map to plots": https://geopandas.readthedocs.io/en/latest/gallery/plotting_basemap_background.html

But this does not seem to solve the problem. It would be great if I can get some suggestions how to solve the issue. I want to overlay the extent map using this background image

srk
  • 21
  • 3

1 Answers1

-1

Try setting the minimum value for your color map and then specifying values below the minimum to be completely transparent.

I'll create an example array with values of either 1 or 2.

import numpy as np
import matplotlib.pyplot as plt
arr = np.ones([64,64])
arr[0:32] = 2
plt.imshow(arr, cmap='viridis')
plt.colorbar()

Figure 1

Now say we want values of 1 (purple) to be transparent in this case. We can do that by setting color map lower limit and then specifying how to map values below the limit:

cmap = plt.get_cmap('viridis')
cmap.set_under('k', alpha=0)
plt.imshow(arr, cmap=cmap, vmin=1.5)
plt.colorbar()

Figure 2

'k' is actually black but alpha=0 makes it transparent

Charles Parr
  • 573
  • 4
  • 16