归一化颜色图的数据

时间:2020-09-29 06:44:22

标签: python matplotlib colormap

我正在绘制带有2个数组数据的大熊猫形状的颜色:

这是我的第一个数组。

     newI = 
    array([ -467,  -415,  -414, -1001,  -246,  -147,  -523,  -327,  -583,
        -541,  -290,  -415,  -453,  -505,  -791,  -812,  -672,  -558,
        -559, -1055,  -327,  -703,  -419,  -499,  -273,  -574,  -802,
        -450,  -743,  -221, -1282,  -704,  -352,  -734,  -430,  -353,
        -515, -1121,  -664,  -586,  -171,  -881,  -402, -1024,  -543,
        -527,  -384,  -775,  -931, -1380, -1662, -1069,  -952,  -435,
       -1051,  -921, -1211,  -794,  -547,  -313,  -511,  -993,  -430,
        -262,  -255,  -675,  -793, -1053,  -702,  -967, -1016,  -230,
        -405,  -869,  -689,  -935,  -190, -1473,  -883, -1233,  -240,
        -607,  -339, -1130,  -909,  -836,  -667,  -457,  -847,  -538,
        -606,  -457,  -800,  -322, -1339,  -691,  -627,  -689,  -365,
        -600,  -289,  -810,  -577,  -187,  -375,  -574,  -426,  -662,
        -695, -1003,   -40, -1012,  -279,  -966,  -587,  -641,  -753,
        -461,  -563,  -604, -1013,  -625,  -506,  -416, -1385,  -459,
        -760,  -347,  -308,  -555,  -325, -1588,  -566,  -533,  -843,
        -501,  -448, -1022,  -654,  -602, -1201,  -814,  -754,  -361,
        -325, -1141,  -725,  -256,  -601,  -379,  -496, -1099, -1101,
        -598,  -442,  -773,  -295, -1292,  -558, -1234,  -868, -1135,
        -251, -1398,  -453,  -563, -1306,  -693,  -560,  -512,  -935,
       -1168,  -482,  -431, -1199, -1249, -1199,  -413, -1018,  -194,
        -375,  -932, -1028,  -436,  -955,  -463, -1303,  -676,  -554,
        -601,  -875,  -661,  -791,  -443,   -89,  -879,  -606,  -577,
        -475,  -802,  -734,  -660,  -684,  -174,  -902, -1241, -1320,
        -575,  -855,  -222,  -890,  -701, -1082,  -531,  -693, -1008,
       -1357,  -433,  -379,  -192,  -343,  -477,  -230,  -938,  -675,
        -798,  -259,  -398,  -778,  -484,  -817,  -453,  -564,  -536,
       -1599,  -968,  -547,  -845, -1592,  -256, -1139,  -229,  -926,
        -474,  -392,  -990,  -295,  -558,  -465,  -497,  -395,  -468,
        -310,  -507, -1205,  -705,  -739,  -609,  -809,  -610,  -421,
       -1057, -2023, -1105,  -618,  -466, -1291,  -616,  -620,  -571,
        -904,  -383,  -544,  -688,  -461,  -769,  -990,  -664,  -405,
        -419,  -852,  -435,  -298,  -782,  -758,  -371,  -813,  -421,
        -594,  -259,  -284,  -215,  -452,  -430,  -936,  -994,  -981,
        -502,  -510,  -671,  -721,  -829,  -523,  -288,  -653,  -493,
        -983, -1205,  -722])

这是我的第二个数组:

    array([-2407, -1992, -3400, -4826, -1544,  -820, -3120, -1469, -2869,
       -3622, -1738, -2122, -2773, -2939, -3558, -3575, -3082, -2494,
       -3591, -5022, -1619, -2608, -3371, -3054, -1596, -2538, -3566,
       -2035, -3490,  -522, -5362, -3055, -1517, -4107, -2039, -2497,
       -2302, -5513, -3876, -4303,  -831, -4457, -2027, -5083, -2716,
       -2284, -1288, -3781, -4707, -6903, -8592, -5763, -4644, -1999,
       -4894, -3190, -6263, -3484, -3090, -1899, -2640, -3940, -2919,
        -629, -2018, -4228, -4075, -5249, -2794, -4061, -4089, -1500,
       -2434, -3867, -3359, -4070, -1472, -7334, -4367, -5422, -1563,
       -3092, -1803, -4664, -4096, -3875, -3061, -1181, -4098, -2850,
       -4356, -2239, -3102, -1498, -6458, -3495, -2863, -3568, -1752,
       -3422, -1768, -3675, -2061,  -919, -1452, -2512, -1924, -3668,
       -3931, -4348,  -284, -6232, -1065, -4261, -2739, -3392, -3962,
       -2369, -2508, -3156, -4759, -3012, -3345, -2566, -7910, -2215,
       -3581, -1357, -2155, -2643, -1420, -7449, -3023, -2982, -4913,
       -2835, -1748, -4679, -2950, -2951, -5515, -4195, -3568, -1746,
       -1437, -5429, -3246, -1556, -2635, -1534, -3553, -4451, -5655,
       -2616, -2724, -4445, -1642, -6640, -2869, -5211, -5014, -4909,
       -1103, -5658, -2096, -2427, -5719, -3152, -2717, -2544, -4226,
       -4813, -2319, -2261, -4844, -5383, -5057, -2981, -5448, -1526,
       -1749, -3550, -3736, -1893, -5812, -2686, -5923, -3145, -3569,
       -2523, -4586, -2931, -4104, -2301,  -666, -4402, -3201, -3171,
       -2598, -4279, -3765, -3024, -3085,  -468, -3732, -5899, -6464,
       -3993, -4583, -1126, -4193, -4214, -3902, -2132, -3712, -4879,
       -6907, -1524, -1987, -1444, -2086, -3229, -1316, -4331, -3150,
       -4449, -1700, -1486, -3650, -2478, -4166, -2618, -3308, -2458,
       -7441, -4452, -2438, -4722, -6949, -1712, -4727,  -792, -4193,
       -1610, -1951, -3965, -1410, -2958, -2167, -2050, -2035, -2152,
       -2236, -3235, -5999, -4024, -3111, -3196, -3881, -2647, -2579,
       -6387, -9912, -4677, -2983, -1913, -7547, -3166, -2990, -2183,
       -3401, -2080, -3056, -2225, -2546, -4421, -3867, -2975, -1552,
       -2090, -3871, -1768, -2032, -3564, -3273, -1579, -4338, -1371,
       -3600, -1253, -2083, -1439, -2281, -2045, -4406, -4380, -4129,
       -2520, -2529, -2108, -3081, -3561, -2601,  -843, -3069, -1852,
       -5888, -5730, -3386])

绘制这些数组数据的代码如下所示。

    area_gpd = gpd.read_file("....shp")
    area_gpd['population'] = newI

    plt.rcParams.update({'font.size':32})
    west,south,east,north = area.unary_union.bounds

    fig,ax = plt.subplots(figsize=(40,40))
    cmap = LinearSegmentedColormap.from_list('mycmap', [ 'green','white'])

    melbourne_gpd.plot(ax=ax, column='population',legend=False,cmap=cmap,zorder=3)
    sm = plt.cm.ScalarMappable(cmap=cmap,\
                          norm=plt.Normalize(vmin=-9912,
                                             vmax=-284))

它可以使事物正常化,因此强度现在有所不同。 有什么功能可以标准化此数据? 我希望对于较大的值,地图会更暗。谁能给我一些建议?

非常感谢

1 个答案:

答案 0 :(得分:0)

我从一个关于stackoverflow的人那里找到了一个很好的解决方案:

import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt


class MidpointNormalize(mpl.colors.Normalize):
    def __init__(self, vmin, vmax, midpoint=0, clip=False):
        self.midpoint = midpoint
        mpl.colors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        normalized_min = max(0, 1 / 2 * (1 - abs((self.midpoint - self.vmin) / (self.midpoint - self.vmax))))
        normalized_max = min(1, 1 / 2 * (1 + abs((self.vmax - self.midpoint) / (self.midpoint - self.vmin))))
        normalized_mid = 0.5
        x, y = [self.vmin, self.midpoint, self.vmax], [normalized_min, normalized_mid, normalized_max]
        return sp.ma.masked_array(sp.interp(value, x, y))


vals = sp.array([[-5., 0], [5, 10]]) 
vmin = -1225
vmax = 669

norm = MidpointNormalize(vmin=vmin, vmax=vmax, midpoint=0)

它将很好地完成您的色图工作。 这是我找到解决方案的地方的链接:Colorplot that distinguishes between positive and negative values