TransWikia.com

Custom Colormap (cmap) rasterio, Python

Geographic Information Systems Asked by azis511 on April 14, 2021

I wish to display custom colours for the pixel values in my raster. How can I create a custom colormap (cmap)?
In the example below, I have specified "magma" as the colour map. However, I wish to define my own colors for specific range/values.

import rasterio
from rasterio.plot import show,show_hist


if __name__ == "__main__":

    input_Dir = 'generated_46.tif'
    src = rasterio.open(input_Dir)
    show(src,cmap="magma")

Here the result of the above code

enter image description here

One Answer

This worked for me

levels = [0, 0.01, 0.02, 0.04, 0.8, 1, 2, 4, 7, 17, 70, 500]
    clrs = ['#E3E8E81A','#3DFDFF8C', '#3CFAFF', '#6A95FF', '#E61AFE8C', '#FF04FF', '#f4ded9', '#f4dbaa', '#eda14f', '#e87511', '#b55400', '#633b11']
    
cmap, norm = colors.from_levels_and_colors(levels, clrs,extend='max')

show(src,cmap=cmap,norm=norm,interpolation='bilinear')

Correct answer by azis511 on April 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP