Geopandas userdefined color scheme drops colors

こ雲淡風輕ζ 提交于 2021-02-11 14:24:30

问题


I expected to get a legend of the three colors green, yellow and red, even if the bottom range is empty (no numbers below 10). Instead GeoPandas drops the yellow color and uses green twice.

Is this a bug or do I miss a parameter?

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},
)


回答1:


I was able to fix this issue by setting the norm Parameter:

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)),
)

I don't fully know what I am doing here. Basically I think I prevent the default behaviour of normalization of the range of colors to a reduced range of numbers. It's a mixture of understanding the source code and plain trial and error. At least it works for my needs.



来源:https://stackoverflow.com/questions/64791779/geopandas-userdefined-color-scheme-drops-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!