3

Data

  • the .tif data DEM was the altitude value represent the hypsography of one administrative division. I upload it here

  • The area besides the division I don't want to show them on the plot.

My aim

Plotting the hypsography of this division using plt.pcolormesh(PS: I found out with reading .tif, plt.imshow() was way faster than pcolormesh. And I don't know Why).

Here I show one example which I clipped from internet.

http://7xrn7f.com1.z0.glb.clouddn.com/16-3-10/56951530.jpg

My attempt

### Using GDAL to read the .tif data
from osgeo import gdal
### Read the .tif
pathToRaster = r'./dem.tif'
raster = gdal.Open(pathToRaster,  gdal.GA_ReadOnly)
dem = raster.GetRasterBand(1).ReadAsArray()
dem = dem[::-1]       

### Mask the outside value
dem_mask = np.ma.masked_less(dem,0)
plt.pcolormesh(dem_mask,cmap =plt.cm.terrain)

Result

http://7xrn7f.com1.z0.glb.clouddn.com/16-3-10/45669007.jpg

Problem

In my research division, this area didn't contain the Sea/Ocean which are plotted by blue above.

But I want to use the plt.cm.terrain as my pcolormesh's colormap because it fit the situation.

So, I want to remove the blue part of the colormap, and start the terrain using green representing the plain.

Han Zhengzu
  • 3,694
  • 7
  • 44
  • 94

1 Answers1

3

I have two solutions right now.

1. Using vmin to set a smaller beginning.

>print dem_mask.min()
>print dem_mask.max()
output: 20
        2271
## Set vmin value far smaller than 20
plt.pcolormesh(dem_mask,cmap =plt.cm.terrain,vmin = -800)       

2. Extract a subset of a colormap referencing to this question

 import matplotlib.colors as colors
 def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
     new_cmap = colors.LinearSegmentedColormap.from_list(
           'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval),
            cmap(np.linspace(minval, maxval, n)))
     return new_cmap    

 cmap = plt.get_cmap('terrain')
 new_cmap = truncate_colormap(cmap, 0.25, 0.9)
 plt.pcolormesh(dem_mask,cmap =new_cmap,)   

Result

http://7xrn7f.com1.z0.glb.clouddn.com/16-3-11/96590416.jpg

Community
  • 1
  • 1
Han Zhengzu
  • 3,694
  • 7
  • 44
  • 94