Geopandas用户定义的配色方案会掉落颜色 [英] Geopandas userdefined color scheme drops colors

查看:170
本文介绍了Geopandas用户定义的配色方案会掉落颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能得到绿色,黄色和红色这三种颜色的传说,即使底部范围是空的(没有低于10的数字).相反,GeoPandas会掉落黄色并使用绿色两次.

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

推荐答案

我能够通过设置 norm 参数来解决此问题:

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.

这篇关于Geopandas用户定义的配色方案会掉落颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆