Geopandas 用户定义的配色方案会掉落颜色

Geopandas userdefined color scheme drops colors

我希望得到绿黄红三种颜色的图例, 即使底部范围为空(没有低于 10 的数字)。 相反,GeoPandas 去掉了黄色并使用了两次绿色。

这是一个错误还是我错过了一个参数?

import pandas as pd
import geopandas
from matplotlib.colors import ListedColormap

colors = ['green', 'yellow', 'red']
bins = [10, 30]
numbers = [15, 25, 35, 35, 55]

ny = geopandas.read_file(geopandas.datasets.get_path('nybb'))
numbers = pd.Series(numbers, name='numbers')
ny = pd.concat([ny, numbers], axis=1)
ny.plot(
    legend=True,
    column='numbers',
    scheme="user_defined",
    cmap = ListedColormap(colors),
    classification_kwds={'bins': bins},
)

我能够通过设置 norm 参数解决这个问题:

import pandas as pd
import geopandas
from matplotlib.colors import ListedColormap
from matplotlib.colors import Normalize

colors = ['green', 'yellow', 'red']
bins = [10, 30]
numbers = [15, 25, 35, 35, 55]

ny = geopandas.read_file(geopandas.datasets.get_path('nybb'))
numbers = pd.Series(numbers, name='numbers')
ny = pd.concat([ny, numbers], axis=1)
ny.plot(
    legend=True,
    column='numbers',
    scheme="user_defined",
    cmap=ListedColormap(colors),
    classification_kwds={ 'bins': bins,  },
    norm=Normalize(0, len(colors)),
)

我不完全知道我在这里做什么。基本上我认为我阻止了将颜色范围标准化为缩小的数字范围的默认行为。它是理解源代码和简单试错的混合体。至少它能满足我的需要。