Python 自定义我自己的调色板并在geopandas地图中使用

Python 自定义我自己的调色板并在geopandas地图中使用,python,geopandas,Python,Geopandas,我想定义一个用于geopandas地图的调色板。我想在RGB 0-0-90和RGB 126-193-61两种颜色之间淡出 我已签出此页面: 但我不明白如何根据这些信息使用定制的颜色 fig, ax = plt.subplots(1, figsize=(10, 16)) matplotlib.rcParams["figure.dpi"] = 100 ax.axis('off') ax.set_title('TITLE', fontdict={'fontsize': '16',

我想定义一个用于geopandas地图的调色板。我想在RGB 0-0-90和RGB 126-193-61两种颜色之间淡出

我已签出此页面: 但我不明白如何根据这些信息使用定制的颜色

fig, ax = plt.subplots(1, figsize=(10, 16))
matplotlib.rcParams["figure.dpi"] = 100
ax.axis('off')
ax.set_title('TITLE', fontdict={'fontsize': '16', 'fontweight' : '3'})
ax.annotate('Källa: Datalagret', xy=(0.7, .05), xycoords='figure fraction', fontsize=11, color='#555555')

sm = plt.cm.ScalarMappable(cmap='GnBu', norm=plt.Normalize(vmin=vmin, vmax=vmax))
fig.colorbar(sm, orientation="horizontal", fraction=0.036, pad=0.015, aspect = 30)

geo_df1.plot(edgecolor='black', column=variable, cmap='GnBu', linewidth=0.2, ax=ax) 

# I'm using GnBu right now, wish to change this to a custom palette.


要从2种给定颜色创建自定义颜色映射,可以使用
ListedColormap
。下面是一个示例代码

import matplotlib
import matplotlib.cm as cm
#from matplotlib.colors import Normalize

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

# Required colors: from_RGB(0-0-90) to_RGB(126-193-61)
RGB1 = [0,0,90]      # dark blue
RGB2 = [126,193,61]  # pale green

N = 256 #number of discrete levels
vals = np.ones((N,4))

vals[:, 0] = np.linspace(RGB1[0]/256, RGB2[0]/256, N)
vals[:, 1] = np.linspace(RGB1[1]/256, RGB2[1]/256, N)
vals[:, 2] = np.linspace(RGB1[2]/256, RGB2[2]/256, N)

# finally, create the required colormap that ranges from
# -- dark blue to pale green
my_cmp = ListedColormap(vals)

# test plot using random data
fig, ax = plt.subplots(figsize=(4, 4))

np.random.seed(1470)
arrdata = 3 + 2.5 * np.random.randn(20, 20)

minv = np.min(arrdata)
maxv = np.max(arrdata)

psm = ax.pcolormesh(arrdata, cmap=my_cmp, rasterized=True, vmin=minv, vmax=maxv)
fig.colorbar(psm, ax=ax)
plt.show()