首页 > 解决方案 > 在 Matplotlib 中更改颜色条缩放

问题描述

使用 NASA 的 SRTM 数据,我生成了一个全球海拔热图。

全球海拔热图

然而,问题是,由于海拔值的范围,大陆往往会与海洋融为一体。是否可以更改颜色条的比例,使大陆的边缘与海洋更加不同?我尝试了不同的 cmap,但它们似乎都遇到了这个问题。

这是我的代码。我正在初始化一个巨大的数组(带有 0)来保存全局高程数据,然后从 SRTM 数据集中逐个文件填充它。每个文件是 1 度纬度乘 1 度经度。

我的另一个问题是关于地图本身。由于某种原因,阿巴拉契亚山脉似乎已经完全消失了。

import os
import numpy as np
from .srtm_map import MapGenerator
from ..utils.hgt_parser import HGTParser
from tqdm import tqdm
import cv2
import matplotlib.pyplot as plt
import richdem as rd

class GlobalMapGenerator():
    def __init__(self):
        self.gen = MapGenerator()
        self.base_dir = "data/elevation/"
        self.hgt_files = os.listdir(self.base_dir)
        self.global_elevation_data = None
    
    def shrink(data, rows, cols):
        return data.reshape(rows, data.shape[0]/rows, cols, data.shape[1]/cols).sum(axis=1).sum(axis=2)

    def GenerateGlobalElevationMap(self, stride):
        res = 1201//stride
        max_N = 59
        max_W = 180
        max_S = 56
        max_E = 179
        
        # N59 --> N00
        # S01 --> S56
        # E000 --> E179
        # W180 --> W001
        
        # Initialize array global elevation
        self.global_elevation_data = np.zeros(( res*(max_S+max_N+1), res*(max_E+max_W+1) ))

        print("Output Image Shape:", self.global_elevation_data.shape)

        for hgt_file in tqdm(self.hgt_files):
            lat_letter = hgt_file[0]
            lon_letter = hgt_file[3]
            lat = int(hgt_file[1:3])
            lon = int(hgt_file[4:7])

            if lat_letter == "S":
                # Shift south down by max_N, but south starts at S01 so we translate up by 1 too
                lat_trans = max_N + lat - 1
            else:
                # Bigger N lat means further up. E.g. N59 is at index 0 and is higher than N00
                lat_trans = max_N - lat
            
            if lon_letter == "E":
                # Shift east right by max_W
                lon_trans = max_W + lon
            else:
                # Bigger W lon means further left. E.g. W180 is at index 0 and is more left than W001
                lon_trans = max_W - lon

            # load in data from file as resized
            data = cv2.resize(HGTParser(os.path.join(self.base_dir, hgt_file)), (res, res))
            
            # generate bounds (x/y --> lon.lat for data from this file for the giant array)
            lat_bounds = [res*lat_trans, res*(lat_trans+1)]
            lon_bounds = [res*lon_trans, res*(lon_trans+1)]
            
            try:
                self.global_elevation_data[ lat_bounds[0]:lat_bounds[1],  lon_bounds[0]:lon_bounds[1] ] = data
            except:
                print("REFERENCE ERROR: " + hgt_file)
                print("lat: ", lat_bounds)
                print("lon: ", lon_bounds)

        # generate figure
        plt.figure(figsize=(20,20))
        plt.imshow(self.global_elevation_data, cmap="rainbow")
        plt.title("Global Elevation Heatmap")
        plt.colorbar()
        plt.show()
        np.save("figures/GlobalElevationMap.npy", self.global_elevation_data)
        plt.savefig("figures/GlobalElevationMap.png")
    
    def GenerateGlobalSlopeMap(self, stride):
        pass

标签: pythonmatplotlibcolorbar

解决方案


推荐阅读