问题
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